How to get hWnd of window opened by ShellExecuteEx.. hProcess?

后端 未结 1 594
长情又很酷
长情又很酷 2020-12-03 22:48

This \"simple\" issue seems to be fraught with side issues.
eg. Does the new process open multiple windows; Does it have a splash screen?
Is there a simple way?

相关标签:
1条回答
  • 2020-12-03 23:07

    First use WaitForInputIdle to pause your program until the application has started and is waiting for user input (the main window should have been created by then), then use EnumWindows and GetWindowThreadProcessId to determine which windows in the system belong to the created process.

    For example:

    struct ProcessWindowsInfo
    {
        DWORD ProcessID;
        std::vector<HWND> Windows;
    
        ProcessWindowsInfo( DWORD const AProcessID )
            : ProcessID( AProcessID )
        {
        }
    };
    
    BOOL __stdcall EnumProcessWindowsProc( HWND hwnd, LPARAM lParam )
    {
        ProcessWindowsInfo *Info = reinterpret_cast<ProcessWindowsInfo*>( lParam );
        DWORD WindowProcessID;
    
        GetWindowThreadProcessId( hwnd, &WindowProcessID );
    
        if( WindowProcessID == Info->ProcessID )
            Info->Windows.push_back( hwnd );
    
        return true;
    }
    
    ....
    
    if( ShellExecuteEx(&sei) )
    {
        WaitForInputIdle( sei.hProcess, INFINITE );
    
        ProcessWindowsInfo Info( GetProcessId( sei.hProcess ) );
    
        EnumWindows( (WNDENUMPROC)EnumProcessWindowsProc,
            reinterpret_cast<LPARAM>( &Info ) );
    
        // Use Info.Windows.....
    }
    
    0 讨论(0)
提交回复
热议问题