C + + Get process number and window

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.