• C++获取进程号及窗口


    复制代码

    #include <TlHelp32.h>
    
    //根据进程名获取进程ID
    BOOL GetPidByProcessName(TCHAR *pProcess, DWORD*dwPid) 
    { 
        HANDLE hSnapshot; 
        PROCESSENTRY32 lppe; 
        //创建系统快照 
        hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); 
    
        if (hSnapshot == NULL) 
            return FALSE; 
    
        //初始化 lppe 的大小 
        lppe.dwSize = sizeof(lppe); 
    
        //查找第一个进程 
        if (!::Process32First(hSnapshot, &lppe))
            return FALSE; 
        do 
        { 
            if(_tcscmp(lppe.szExeFile, pProcess) == 0)
            { 
                *dwPid =  lppe.th32ProcessID;
            }   
        } 
        while (::Process32Next(hSnapshot, &lppe)); //查找下一个进程  
    
        return TRUE;
    }
    
    //根据进程ID获取窗口句柄
    HWND GetHwndByPid(DWORD dwProcessID)
    {
        //返回Z序顶部的窗口句柄
        HWND hWnd = ::GetTopWindow(0);
    
        while ( hWnd )
        {
            DWORD pid = 0;
            //根据窗口句柄获取进程ID
            DWORD dwTheardId = ::GetWindowThreadProcessId( hWnd,&pid);
    
            if (dwTheardId != 0)
            {
                if ( pid == dwProcessID )
                {
                    return hWnd;
                }
            }
            //返回z序中的前一个或后一个窗口的句柄
            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;
    }

  • 相关阅读:
    ArduinoYun教程之ArduinoYun硬件介绍
    Nmap扫描教程之基础扫描详解
    Nmap扫描教程之Nmap基础知识
    Nmap扫描教程之DNS服务类
    Nmap扫描教程之网络基础服务DHCP服务类
    Xamarin iOS教程之自定义视图
    Xamarin iOS教程之警告视图
    Xamarin iOS教程之页面控件
    使用光学鼠标传感器实现旋转(或线性)测量(转)
    基于STM32的uCOS-II移植详解
  • 原文地址:https://www.cnblogs.com/liangxiaofeng/p/4316109.html
Copyright © 2020-2023  润新知