Search code examples
winapiwin32-process

Identify which executable is throwing a runtime error


I have a program that is throwing a run-time error dialog. Is there a way to programmatically detect which executable is the parent of this dialog box from another process?


Solution

  • 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.