[C++] Win32: How to Find Window Handle by Process ID and Window Class Name
HWND FindWindowByProcessIdAndClassName(DWORD pid, TCHAR* szWndClassName)
{
HWND hCurWnd = GetTopWindow(0);
while (hCurWnd != NULL)
{
DWORD cur_pid;
DWORD dwTheardId = GetWindowThreadProcessId(hCurWnd, &cur_pid);
if (cur_pid == pid)
{
if (IsWindowVisible(hCurWnd) != 0)
{
TCHAR szClassName[256];
GetClassName(hCurWnd, szClassName, 256);
if (_tcscmp(szClassName,szWndClassName)==0)
return hCurWnd;
}
}
hCurWnd = GetNextWindow(hCurWnd, GW_HWNDNEXT);
}
return NULL;
}