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/*your process id*/ ) { retHwnd = h; //会有多个相等值 // /* char buf[MAX_PATH] = {0}; sprintf(buf,"%0x",h); MessageBox(NULL,buf,"提示",MB_OK); // */ } } h = GetNextWindow(h,GW_HWNDNEXT); } return retHwnd; }
网上的基本是全身错误的那个版本。