I have a program that is throwing a run-time error dialog. Is there a way to programmatically detect which executable is the p开发者_如何学JAVAarent of this dialog box from another process?
Yes. The code would be something like this (error checking omitted):
HWND hWindow = FindWindow( NULL, windowName );
DWORD processId;
GetWindowTheadProcessId( hWindow, &processId );
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, processId );
GetModuleFileNameEx( hProcess, NULL, buffer, BUFFER_SIZE );
The GetProcessImageFileName() function is preferrable to GetModuleFileNameEx() if you aren't concerned with older platforms.
Update: You can get a process name from a window handle using GetWindowModuleFileName
To find the find handle, you could get the DesktopWindow and then enumerate the ones until you find the one with the error message.
精彩评论