有时候进程未正常退出,导致进程列表遗留僵尸进程,程序启动需要杀死这种僵尸进程。
#include "TLHELP32.H" void TerminateSelfApplication() { TCHAR szFileName[MAX_PATH] = { 0 }; TCHAR* szAppName = NULL; if (::GetModuleFileName(NULL, szFileName, MAX_PATH)) { szAppName = _tcsrchr(szFileName, '\') + 1; } else { return; } HANDLE Snapshot; Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 processListStr; processListStr.dwSize = sizeof(PROCESSENTRY32); BOOL return_value; return_value = Process32First(Snapshot, &processListStr); while (return_value) { if (_tcsicmp(processListStr.szExeFile, szAppName) == 0) { if (processListStr.th32ProcessID != GetCurrentProcessId()) { TCHAR szCommand[MAX_PATH] = { 0 }; _stprintf_s(szCommand, sizeof(szCommand), _T("taskkill /f /t /pid %d"), processListStr.th32ProcessID); ShellExecute(NULL, _T("open"), szCommand, NULL, NULL, SW_HIDE); // 或以下方法 //DWORD dwThreadID = GetCurrentProcessId(); //HANDLE ProcessHandle = OpenProcess(PROCESS_TERMINATE, FALSE, processListStr.th32ProcessID);//杀进程 //TerminateProcess((HANDLE)ProcessHandle, 0); } } return_value = Process32Next(Snapshot, &processListStr); } CloseHandle(Snapshot); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)