STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
char * szCommandLine = "cmd";
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
BOOL bRet =::CreateProcess(
NULL,
szCommandLine,
NULL,
NULL,
0,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
if(bRet)
{
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
printf("新进程的 进程ID为 %d \n",pi.dwProcessId);
printf("新线程的 线程ID为 %d \n",pi.dwThreadId);
}
int index=0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp32Snapshow error!");
return -1;
}
BOOL bMore = ::Process32First(hProcessSnap,&pe32);
//::ExitProcess(0);
while(bMore)
{
printf("**************%d**************\n",++index);
printf("process name :%s \n",pe32.szExeFile);
printf("process ID :%d \n",pe32.th32ProcessID);
printf("thread nums:%d \n",pe32.cntThreads);
if(!strcmp(pe32.szExeFile,"QQ.exe"))
{
//::EnumProcesses(
DWORD dExitCode;
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);
::GetExitCodeProcess(hProcess,&dExitCode);
if(STILL_ACTIVE==dExitCode)
{
printf("%s存在!\n",pe32.szExeFile);
}
else
{
printf("%s不存在!\n",pe32.szExeFile);
}
if(hProcess!=NULL)
{
TerminateProcess(hProcess,0);
printf("######结束%s成功!########\n",pe32.szExeFile);
}
::GetExitCodeProcess(hProcess,&dExitCode);
if(STILL_ACTIVE==dExitCode)
{
printf("%s存在!\n",pe32.szExeFile);
}
else
{
printf("%s不存在!\n",pe32.szExeFile);
}
::CloseHandle(hProcess);
}
printf("进程创建线程的基本优先级 %d\n\n",pe32.pcPriClassBase);
bMore = ::Process32Next(hProcessSnap,&pe32);
}
::CloseHandle(hProcessSnap);