c++ - 如何获取 ShellExecuteEx.. hProcess 打开的窗口的 hWnd?

标签 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?
}
...

最佳答案

首先使用 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.....
}

关于c++ - 如何获取 ShellExecuteEx.. hProcess 打开的窗口的 hWnd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3269390/

相关文章:

windows - 创建一个防弹工作进程(在 windows 上)

c# - 重新启动当前进程 C#

java - Java 中的 "Math.random()"问题

c++ - 无法捕获自定义 std::runtime_error

c++ - 我的二维数组太大了吗?

c++ - protobuf 抛出运行时错误,因为无法解析最新版本 3.0.0 中的地址簿?

windows - 如何改变 explorer.exe 和 service.exe 的启动方式来编辑进程关联?

c++ - 使用 std::fill 初始化结构对象

c# - 在桌面上拖动 WPF 窗体

linux - bash,查找聚焦窗口的顺序(类似于 alt+tab 菜单)