mfc 根據視窗控制代碼(HWND)殺死程序
阿新 • • 發佈:2019-01-28
先列舉桌面所有的視窗的標題或者類名。根據類名或者標題得知該視窗的控制代碼(HWND)。然後再殺死程序。看程式碼:
BOOL bTerminateProcess = FALSE; HWND hwndException = NULL; BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam) { char wcText[MAX_PATH * 8] = {0}; GetWindowText(hwnd, wcText, MAX_PATH * 8); if (_stricmp(wcText, "CarCapture.exe - 應用程式錯誤") == 0 || _stricmp(wcText, "Microsoft Visual C++ Runtime Library") == 0){ hwndException = hwnd; bTerminateProcess = TRUE; } return true; }
BOOL bRet = EnumWindows(EnumWindowsProc, NULL); if (bTerminateProcess){ { DWORD dwPID = 0; GetWindowThreadProcessId(hwndException, &dwPID); char strCmd[MAX_PATH] = {0}; sprintf(strCmd, "taskkill /pid %d -f", dwPID); system(strCmd); } TerminateProcess(GetCurrentProcess(), 0); }
對於XP下面的csrss異常,類似這樣的:
可以使用::SendMessage(hwndException, WM_CLOSE, 0, 0);來結束它