HWND GetProcessTopWnd2(IN DWORD dwPid)
{
HWND ret = 0;
try
{
DWORD dwEnumPid = 0;
HWND hEnumWnd = GetTopWindow(0);
while (hEnumWnd)
{
::GetWindowThreadProcessId(hEnumWnd, &dwEnumPid);
if (dwPid == dwEnumPid)
{
LONG lStyle = ::GetWindowLong(hEnumWnd, GWL_STYLE);
if ((lStyle & WS_VISIBLE) && (GetParent(hEnumWnd) == 0)) /*顶层可见窗口*/
{
ret = hEnumWnd;
break;
}
}
hEnumWnd = GetWindow(hEnumWnd, GW_HWNDNEXT);
}
}
catch (...)
{
OutputDebugStringA(__FUNCTION__);
}
return ret;
}
int main()
{
STARTUPINFO si = { 0 };
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 };
BOOL b = CreateProcess(_T("C:\\Using\\winmine.exe"), 0, 0, 0, 0, 0, 0, 0, &si, &pi);
HWND h = 0;
if (b)
{
for (int i = 0; i < 5 && !h; ++i)
{
h = GetProcessTopWnd2(pi.dwProcessId);
Sleep(1000);
}
printf("%d %08X \r\n", h, h);
}
return 0;
}
原文:https://www.cnblogs.com/dailycode/p/12460178.html