天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 56|回复: 0

获取所有的CAD的进程号,窗口句柄,类名,窗口标题

[复制链接]
  • TA的每日心情
    开心
    昨天 15:13
  • 签到天数: 153 天

    [LV.7]常住居民III

    3456

    主题

    553

    回帖

    214748万

    积分

    管理员

    中国膜结构网www.mjgou.com

    积分
    2147483647
    QQ
    发表于 2024-7-13 22:19:57 | 显示全部楼层 |阅读模式
    1. #include "stdafx.h"
    2. #include "TlHelp32.h"
    3. #include "GetProcess.h"
    4. #include "Psapi.h"
    5. #include "winver.h"
    6. #include "MSCorEE.h"

    7. CString strWindows = _T("");
    8. #pragma comment(lib, "version.lib")
    9. #pragma comment(lib, "Psapi.lib")
    10. #pragma comment(lib, "mscoree.lib")

    11. struct LANGANDCODEPAGE
    12. {
    13.         WORD wLanguage;
    14.         WORD wCodePage;
    15. } *lpTranslate;

    16. //----------------------------------------------------------------------------------
    17. //另外方法
    18. //枚举回调函数
    19. BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)
    20. {
    21.         DWORD dwProcessId(0);
    22.         LPWNDINFO pInfo = (LPWNDINFO)lParam;
    23.         //通过窗口句柄获取进程ID
    24.         GetWindowThreadProcessId(hWnd, &dwProcessId);
    25.         TCHAR szText[MAX_PATH];
    26.         ::GetWindowText(hWnd,szText,MAX_PATH);
    27.         //匹配遍历窗口进程号与通过进程名得到的进程号
    28.         TRACE( _T("EnumWindowsProc..hWnd=0x%x, ProcessID=%d, WindowsText=%s \n"), hWnd, dwProcessId, szText );
    29.         if(dwProcessId == pInfo->dwProcessId && IsWindowVisible(hWnd))
    30.         {
    31.                 TCHAR szClassName[80];
    32.                 GetClassName(hWnd,szClassName,80);
    33.                 HWND hParent = (HWND)::GetWindowLong(hWnd,-8);   //GWL_HWNDPARENT ;_WIN64,  GWLP_HWNDPARENT
    34.                 if (hParent == 0 && _tcsncmp(szClassName,_T("Afx"),3) == 0 )
    35.                 {
    36.                         strWindows +=_T("\n");
    37.                         strWindows += szText;
    38.                         //CString strTemp;
    39.                         //strTemp.Format(_T("窗口句柄:%ld,父窗口句柄:%ld,窗口文本%s,窗口类别:%s"),hWnd,hParent,szText,szClassName);
    40.                         //AfxMessageBox(strTemp);
    41.                         pInfo->hWnd = hWnd;
    42.                         pInfo->strClassName = szClassName;
    43.             pInfo->strWindowText = szText;
    44.                         return FALSE;
    45.                 }
    46.         }
    47.         return TRUE;
    48. }

    49. //获取所有的CAD的进程号,窗口句柄,类名,窗口标题,
    50. int GFindWindows(LPCTSTR proc_name,vector<WNDINFO>  &WndSet)
    51. {
    52.         int num=0;//返回的窗口句柄数目
    53.         WndSet.clear();

    54.         DWORD dwPID = 0;  //一个临时PID
    55.         int a[MAX_PATH];//存放进程PID的数组
    56.         DWORD Proc_num=0;//进程数量
    57.         CString procname(proc_name);
    58.         //匹配进程名是否含 exe
    59.         if (_tcscmp(procname.Right(procname.GetLength() - procname.ReverseFind('.') - 1),_T("exe")))
    60.         {       
    61.                 return -1;//参数错误返回-1
    62.         }
    63.         //************************根据进程名称获取进程ID***********//
    64.         HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    65.         if (INVALID_HANDLE_VALUE == hSnapshot)
    66.         {
    67.                 return 0;
    68.         }
    69.         PROCESSENTRY32 pe = { sizeof(pe) };
    70.         BOOL fOk;
    71.         for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
    72.         {
    73.                 CString temp;
    74.                 TRACE(_T("当前查找进程的名称是: %s\n"),pe.szExeFile);
    75.                 TRACE(_T("当前进程PID: %d \n"),pe.th32ProcessID);
    76.                 if (!_tcscmp(pe.szExeFile, proc_name))
    77.                 {
    78.                         //CloseHandle(hSnapshot);
    79.                         temp.Format(_T("当前进程PID: %d \n"),pe.th32ProcessID);
    80.                         TRACE(_T("当前匹配的进程PID: %d \n"),pe.th32ProcessID);
    81.                         a[Proc_num] = pe.th32ProcessID;
    82.                         Proc_num++;
    83.                 }
    84.         }
    85.         //进程数量为0 提示找不到进程 返回为0;
    86.         if (Proc_num==0)
    87.         {
    88.                 return 0;
    89.         }
    90.         else//找到一个匹配进程
    91.         {
    92.                 strWindows.Empty();
    93.                 WNDINFO wi;
    94.                 //对一个进程名有多个相同进程ID的全部数组进行处理
    95.                 for (DWORD j=0;j<Proc_num;j++)
    96.                 {
    97.                         wi.hWnd = NULL;
    98.                         wi.dwProcessId =a[j];//将获取到的进程ID传给结构体
    99.                         wi.strAppPath = GetProcessPath(a[j]);
    100.                         wi.strVersion = GetAppVersion(wi.strAppPath);
    101.                 //遍历顶层窗口获取窗口句柄
    102.                         EnumWindows(EnumWindowsProc,(LPARAM)&wi);
    103.                         //判断当前进程是否无窗口 无窗口句柄则不保存
    104.                         if (wi.hWnd!=NULL)
    105.                         {
    106.                                 WndSet.push_back(wi);                //如果是需要的进程,则加入!!!!!!
    107.                         }
    108.                 }
    109.                 //AfxMessageBox(strWindows);
    110.                 return num;//返回句柄个数
    111.         }
    112. }

    113. //获取文件版本号
    114. CString GetAppVersion(LPCTSTR pcszFileName)
    115. {
    116.         if (_tcscmp(pcszFileName,_T("X64")) == 0)
    117.         {
    118.                 return pcszFileName;
    119.         }
    120.     //上面的小段为我加

    121.         DWORD dwSize = 0;
    122.         DWORD uiSize = GetFileVersionInfoSize(pcszFileName,&dwSize);
    123.         if (0 == uiSize)
    124.         {
    125.                 //0 意味着GetFileVersionInfoSize 函数调用失败
    126.                 return _T("");
    127.         }

    128.         PTSTR pBuffer = new TCHAR[uiSize];

    129.         if (NULL == pBuffer)
    130.         {
    131.                 //分配内存失败:)
    132.                 return _T("");
    133.         }

    134.         memset((void*)pBuffer,0,uiSize);

    135.         //获取exe 或 DLL 的资源信息,存放在pBuffer内
    136.         if(!GetFileVersionInfo(pcszFileName,0,uiSize,(PVOID)pBuffer))
    137.         {
    138.                 //GetFileVersionInfo 调用失败.
    139.                 delete []pBuffer;
    140.                 return _T("");
    141.         }


    142.         LANGANDCODEPAGE *pLanguage = NULL;  //这里这样设置没关系了。

    143.         UINT  uiOtherSize = 0;
    144.         //获取资源相关的 codepage 和language
    145.         if (!VerQueryValue(pBuffer,_T("\\VarFileInfo\\Translation"),
    146.                 (PVOID*)&pLanguage,&uiOtherSize))
    147.         {
    148.                 //出错
    149.                 delete []pBuffer;
    150.                 return _T("");
    151.         }

    152.         //////////////////////////////////////////////////////////////////////////
    153.         //////////////////////////////////////////////////////////////////////////
    154.         //超级重点

    155.         LPVOID pTmp = NULL;   //一定要把pTmp这个变量设置成PVOID或LPVOID型的
    156.         //否则无法获取信息。你不信可以试。
    157.         //TCHAR *pTmp = NULL;
    158.         //或下面这样的设置
    159.         //TCHAR pTmp[MAX_PATH];
    160.         //memset((void*)pTmp,0,sizeof(pTmp));

    161.         //////////////////////////////////////////////////////////////////////////
    162.         //////////////////////////////////////////////////////////////////////////

    163.         TCHAR SubBlock[MAX_PATH];
    164.         memset((void*)SubBlock,0,sizeof(SubBlock));

    165.         CString strVersion;
    166.        
    167.         for(UINT i=0; i < (uiOtherSize / sizeof(LANGANDCODEPAGE)); i++ )
    168.         {
    169.                 //获取每种 CodePage 和 Language 资源的相关信息
    170.                 wsprintf(SubBlock,
    171.                         TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"),
    172.                         pLanguage[i].wLanguage,
    173.                         pLanguage[i].wCodePage);

    174.                 //   Comments InternalName ProductName
    175.                 //   CompanyName LegalCopyright ProductVersion
    176.                 //   FileDescription LegalTrademarks PrivateBuild
    177.                 //   FileVersion OriginalFilename SpecialBuild  
    178.                 //         OriginalFilename 可由上面的各种代替。
    179.                 //   Retrieve file description for language and code page "i".
    180.                  
    181.                 int nRet = VerQueryValue(pBuffer,SubBlock, (LPVOID*)&pTmp, &uiOtherSize);
    182.                 if (nRet != 0)
    183.                 {
    184.                         strVersion.Format(_T("%s"),(TCHAR*)pTmp);
    185.                 }
    186.         }
    187.         delete []pBuffer;
    188.         pBuffer = NULL;
    189.         return strVersion;
    190. }

    191. //获取进程路径
    192. CString GetProcessPath( DWORD idProcess )
    193. {
    194.         // 获取进程路径
    195.         TCHAR szProcessName[MAX_PATH] = _T("");
    196.         // 打开进程句柄,需要管理员权限
    197.         HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, idProcess );
    198.         if( NULL != hProcess )
    199.         {
    200.                 HMODULE hMod;
    201.                 DWORD cbNeeded;
    202.                 // 获取路径
    203.                 if( EnumProcessModules( hProcess, &hMod, (DWORD)sizeof( hMod ), (LPDWORD)&cbNeeded ) )
    204.                 {
    205.                         DWORD dw = GetModuleFileNameEx( hProcess, hMod, szProcessName, MAX_PATH );
    206.                 }
    207.                 else
    208.                 {
    209.                         //此处最可能碰到299错误,即如果编译成32位程序,去调用64位CAD
    210.                         TRACE(_T("出错代码是:%d"),GetLastError());
    211.                         int nError = GetLastError();  //WSAGetLastError
    212.                         if (nError == 299)
    213.                         {
    214.                                 CloseHandle(hProcess);
    215.                                 return _T("X64");                        //64位的CAD
    216.                         }
    217.                 }
    218.                 CloseHandle( hProcess );
    219.         }
    220.         return (szProcessName);
    221. }


    222. //将wchar_t* 转成char*的实现函数如下:
    223. char *w2c(const wchar_t *pwstr)
    224. {
    225.         //size_t nlength=wcslen(pwstr);
    226.         //获取转换后的长度
    227.         size_t nbytes = WideCharToMultiByte( 0, // specify the code page used to perform the conversion
    228.                 0,         // no special flags to handle unmapped characters
    229.                 pwstr,     // wide character string to convert
    230.                 -1,//nlength,   // the number of wide characters in that string
    231.                 NULL,      // no output buffer given, we just want to know how long it needs to be
    232.                 0,
    233.                 NULL,      // no replacement character given
    234.                 NULL );    // we don't want to know if a character didn't make it through the translation
    235.         // make sure the buffer is big enough for this, making it larger if necessary
    236.         //if( nbytes > len)   
    237.                 //nbytes=len;

    238.         char *pcstr = new char[nbytes];
    239.         // 通过以上得到的结果,转换unicode 字符为ascii 字符
    240.         WideCharToMultiByte( 0, // specify the code page used to perform the conversion
    241.                 0,         // no special flags to handle unmapped characters
    242.                 pwstr,   // wide character string to convert
    243.                 -1,//nlength,   // the number of wide characters in that string
    244.                 pcstr, // put the output ascii characters at the end of the buffer
    245.                 nbytes,                           // there is at least this much space there
    246.                 NULL,      // no replacement character given
    247.                 NULL );
    248.         return pcstr ;
    249. }

    250. //把ascii 字符转换为unicode字符
    251. wchar_t* c2w(wchar_t *pwstr, const char *str)
    252. {
    253.         wchar_t* buffer=NULL;
    254.         if(str != NULL)
    255.         {
    256.                 size_t nu = strlen(str);
    257.                 size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,int(nu),NULL,0);
    258.                 buffer = new wchar_t[n+1];
    259.                 ::MultiByteToWideChar(CP_ACP,0,(const char *)str,int(nu),buffer,int(n));   
    260.         }
    261.         return buffer;
    262. }
    复制代码

     

     

     

     

    获取所有的CAD的进程号,窗口句柄,类名,窗口标题
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池| |网站地图

    GMT+8, 2024-9-8 09:10 , Processed in 0.059686 second(s), 23 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表