#include <tlhelp32.h>//get process idbool getpidbyprocessname (TCHAR *pprocess, dword*dwpid) According to the process name {HANDLE hsnapshot; PROCESSENTRY32 Lppe; Create a system snapshot hSnapShot =:: CreateToolhelp32Snapshot (Th32cs_snapprocess, NULL); if (hSnapShot = = NULL) return FALSE; Initializes the size of the Lppe lppe.dwsize = sizeof (Lppe); Find the first process if (!::P Rocess32first (hSnapShot, &lppe)) return FALSE; do {if (_tcscmp (lppe.szexefile, pprocess) = = 0) {*dwpid = Lppe.th32processid; }} while (::P rocess32next (hSnapShot, &lppe)); Find Next process return TRUE;} Gets the window handle based on the process ID hwnd gethwndbypid (DWORD dwprocessid) {//Returns the window handle at the top of the Z-order hwnd hwnd =:: Gettopwindow (0); while (hWnd) {DWORD pid = 0; Get process ID based on window handle DWORD Dwtheardid =:: GetWindowThreadProcessId (Hwnd,&pid); if (Dwtheardid! = 0) {if (PID = = Dwprocessid) {return hWnd; } } Returns the handle of the previous or next window in the z-order hWnd =:: Getnextwindow (hwnd, gw_hwndnext); } return hWnd;
typedef struct
{
HWND hwnd;
DWORD Dwpid;
}wndinfo;
BOOL CALLBACK Enumwindowsproc (HWND hwnd, LPARAM LPARAM)
{
wndinfo* PInfo = (wndinfo*) LParam;
DWORD dwprocessid = 0;
GetWindowThreadProcessId (HWnd, &dwprocessid);
if (Dwprocessid = = pinfo->dwpid)
{
Pinfo->hwnd = hWnd;
return FALSE;
}
return TRUE;
}
HWND Gethwndbyprocessid (DWORD dwprocessid)
{
Wndinfo info = {0};
Info.hwnd = NULL;
Info.dwpid = Dwprocessid;
EnumWindows (Enumwindowsproc, (LPARAM) &info);
return info.hwnd;
}
C + + Get process number and window