如何通过ShellExecuteEx打开hWnd窗口.. hProcess?

时间:2010-07-16 22:54:03

标签: c++ process window handle

这个“简单”的问题似乎充满了各方面的问题   例如。新进程是否打开了多个窗口;它有闪屏吗?
  有一个简单的方法吗? (我正在开始一个新的Notepad ++实例)

...
std::tstring  tstrNotepad_exe = tstrProgramFiles + _T("\\Notepad++\\notepad++.exe");

SHELLEXECUTEINFO SEI={0};
sei.cbSize       = sizeof(SHELLEXECUTEINFO);
sei.fMask        = SEE_MASK_NOCLOSEPROCESS;
sei.hwnd         = hWndMe;  // This app's window handle
sei.lpVerb       = _T("open");
sei.lpFile       = tstrNotepad_exe.c_str();     
sei.lpParameters = _T(" -multiInst -noPlugins -nosession -notabbar ";   
sei.lpDirectory  = NULL;
sei.nShow        = SW_SHOW;
sei.hInstApp     = NULL;    
if( ShellExecuteEx(&sei) )
{ // I have sei.hProcess, but how best to utilize it from here?
}
...

1 个答案:

答案 0 :(得分:13)

首先使用WaitForInputIdle暂停程序直到应用程序启动并等待用户输入(主窗口应该在那时创建),然后使用EnumWindowsGetWindowThreadProcessId确定系统中的哪些窗口属于创建的进程。

例如:

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