先枚举桌面所有的窗口的标题或者类名。根据类名或者标题得知该窗口的句柄(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);来结束它