建立進程 和 列出所有進程

來源:互聯網
上載者:User
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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.