VS2019 用进程pid取主窗口句柄hwnd代码分享
网上的基本是全身错误的那个版本,这个代码是经过完善优化后,正确的代码。
HWND GetHwndByPid(DWORD dwProcessID)
{
HWND h = GetTopWindow(0);
HWND retHwnd = NULL;
while ( h )
{
DWORD pid = 0;
DWORD dwTheardId = GetWindowThreadProcessId( h,&pid);
if (dwTheardId != 0)
{
if ( pid == dwProcessID && GetParent(h)==NULL && ::IsWindowVisible(h))
{
retHwnd = h; //会有多个相等值
// /*
char buf = {0};
sprintf(buf,"%0x",h);
MessageBox(NULL,buf,"提示",MB_OK);
// */
}
}
h= GetNextWindow(h,GW_HWNDNEXT);
}
return retHwnd;
}
页:
[1]