- 逻辑:
比较简单的一个逻辑是 循环遍历进程id即可。 - 代码:
//监听进程id是否退出
//@params:cur_pid:进程id
//@return:退出时候,会弹弹窗提示
bool ListenExitProcesses(DWORD cur_pid)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE) {
cout << "CreateToolhelp32Snapshot Error!" << endl;;
return false;
}
BOOL bResult =Process32First(hProcessSnap, &pe32);
BOOL bContinue = true; //为false才退出
bool bRe = true;
while (bContinue)
{
bRe = true;
//需要重新获取进程id
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
bResult = Process32First(hProcessSnap, &pe32); //遍历第一个
while (bResult)
{
int id = pe32.th32ProcessID;
if (id == cur_pid)
{
bResult = false;
bRe = false;
break;
}
bResult = Process32Next(hProcessSnap,&pe32);
}
if (bRe== true)
{
MessageBox(0,L"进程退出",L"进程退出",0);
//break;
bContinue = false;
break;
}
}
CloseHandle(hProcessSnap);
return true;
}