C++ 如何获得当前电脑里运行的所有进程ID?并关闭指定的进程?
展开全部
HANDLE GetProcessHandle(int nID)
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, nID);
}
HANDLE GetProcessHandle(LPCTSTR pName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)) {
if (!_tcscmp(pe.szExeFile, pName)) {
CloseHandle(hSnapshot);
return GetProcessHandle(pe.th32ProcessID);
}
}
return NULL;
}
关闭进程
int killpro(CString a)//CString a里面就是进程名字。
{
PROCESSENTRY32 pe32;
pe32.dwSize =sizeof(pe32);
HANDLE hpro=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hpro==INVALID_HANDLE_VALUE)
{
return 0;
}
BOOL nowrun=Process32First(hpro,&pe32);
while(nowrun)
{
if(pe32.szExeFile ==a)
{
DWORD proid=pe32.th32ProcessID;
HANDLE hprocess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,proid);
if(hprocess!=NULL)
{
::TerminateProcess(hprocess,0);
::CloseHandle(hprocess);
}
}
nowrun=::Process32Next(hpro,&pe32);
}
}
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, nID);
}
HANDLE GetProcessHandle(LPCTSTR pName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)) {
if (!_tcscmp(pe.szExeFile, pName)) {
CloseHandle(hSnapshot);
return GetProcessHandle(pe.th32ProcessID);
}
}
return NULL;
}
关闭进程
int killpro(CString a)//CString a里面就是进程名字。
{
PROCESSENTRY32 pe32;
pe32.dwSize =sizeof(pe32);
HANDLE hpro=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hpro==INVALID_HANDLE_VALUE)
{
return 0;
}
BOOL nowrun=Process32First(hpro,&pe32);
while(nowrun)
{
if(pe32.szExeFile ==a)
{
DWORD proid=pe32.th32ProcessID;
HANDLE hprocess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,proid);
if(hprocess!=NULL)
{
::TerminateProcess(hprocess,0);
::CloseHandle(hprocess);
}
}
nowrun=::Process32Next(hpro,&pe32);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询