在桌面应用中,我使用ActivateApplication启动了一个Metro应用,我想将out参数进程ID转换为进程句柄,以便我可以使用WaitForSingleObjectEx或GetExitCodeProcess。我看到OpenProcess应该完全符合我的要求,但它不起作用。返回的句柄始终为null。我猜两种可能性之一:
答案 0 :(得分:0)
您可能想尝试这样做,循环浏览窗口并比较进程ID以找到正确的句柄。
HWND GetProcessWindow( DWORD processId )
{
// Now need to run a loop to get the correct window for process.
bool bFound = false;
HWND prevWindow = 0;
while ( ! bFound ) {
HWND desktopWindow = GetDesktopWindow();
if ( ! desktopWindow )
break;
HWND nextWindow = FindWindowEx( desktopWindow, prevWindow, NULL, NULL );
if ( ! nextWindow )
break;
// Check whether window belongs to the correct process.
DWORD procId = -1;
GetWindowThreadProcessId( nextWindow, &procId );
if ( procId == processId ) {
// Add additional checks. In my case, I had to bring the window to front so these checks were necessary.
wchar_t windowText[ 1024 ];
if ( IsWindowVisible( nextWindow ) && ! IsIconic( nextWindow ) && GetWindowText( nextWindow, ( LPWSTR )windowText, sizeof( windowText ) / sizeof( wchar_t ) )
&& ! GetParent( nextWindow ) )
return nextWindow;
}
prevWindow = nextWindow;
}
return 0;
}