浏览代码

整理函数:
GetProcessId和GetProcessWnd

Jeff 4 年之前
父节点
当前提交
8e4ab62070
共有 2 个文件被更改,包括 92 次插入0 次删除
  1. 85 0
      GameAssist/GameAssist/Assist.cpp
  2. 7 0
      GameAssist/GameAssist/Assist.h

+ 85 - 0
GameAssist/GameAssist/Assist.cpp

@@ -243,6 +243,91 @@ namespace GAssist
 		return hwndRet;
 		return hwndRet;
 	}
 	}
 
 
+	
+	DWORD GetProcessId(LPCTSTR lpProcessName /* = _T("Game.exe") */)
+	{
+		DWORD dwProcessId = 0;
+		PROCESSENTRY32	pe32 = { 0 };
+
+		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+		if ( hProcessSnap == NULL ) return 0;
+
+		// 遍历进程;
+		pe32.dwSize = sizeof(PROCESSENTRY32);
+		for ( BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32) ) {
+			if ( _tcscmp(lpProcessName, pe32.szExeFile) == 0 ) {
+				dwProcessId = pe32.th32ProcessID;
+				break;
+			}
+		}
+
+		CloseHandle(hProcessSnap);
+
+		return dwProcessId;
+	}
+
+	HWND GetProcessWnd(const DWORD &dwTagetProcessId, LPCTSTR lpTagetWndName /* = _T("大话水浒") */)
+	{
+		DWORD dwCurPorcessId = 0;
+		HWND hTagetProcessWnd = NULL;
+		TCHAR szWndName[MAX_PATH] = { 0 };
+		TCHAR szClassName[MAX_PATH] = { 0 };
+
+		// 取得第一个窗口句柄;
+		for ( HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT) ) {
+			// 重置为0;
+			dwCurPorcessId = 0;
+			// 通过窗口句柄反查进程pid;
+			DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
+			if ( dwThreadId != 0 ) {
+				// 判断当前进程id是否和目标进程id相同;
+				if ( dwCurPorcessId == dwTagetProcessId ) {
+					if ( lpTagetWndName == NULL ) {
+						hTagetProcessWnd = hCurWnd;
+						break;
+					}
+					else{
+						// 获取窗口名称;
+						::GetWindowText(hCurWnd, szWndName, sizeof(szWndName)/sizeof(TCHAR));
+						// 获取窗口类名;
+						::GetClassName(hCurWnd, szClassName, sizeof(szClassName)/sizeof(TCHAR));
+#ifdef _DEBUG
+						TCHAR szLogMsg[MAX_PATH] = {0};
+						_stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClassName, szWndName, hCurWnd);
+						OutputDebugString(szLogMsg);
+#endif
+						if ( _tcsstr(szWndName, lpTagetWndName) != NULL ) {
+							hTagetProcessWnd = hCurWnd;
+							break;
+						}
+					}
+				}
+			}
+		}
+
+		// 当前窗口有可能不是进程父窗口;
+		HWND hParentWnd = hTagetProcessWnd;
+		while ( hParentWnd ) {
+			hParentWnd = ::GetParent(hTagetProcessWnd);
+			if ( hParentWnd == NULL )
+				break;
+			hTagetProcessWnd = hParentWnd;
+		}
+
+		return hTagetProcessWnd;
+	}
+
+	HWND GetProcessWnd(LPCTSTR lpProcessName /* = _T("Game.exe") */, LPCTSTR lpTagetWndName /* = _T("大话水浒") */)
+	{
+		HWND hTagetWnd = NULL;
+		DWORD dwProcessId = 0;
+		if ( (dwProcessId = GetProcessId(lpProcessName)) != 0 ) {
+			hTagetWnd = GetProcessWnd(dwProcessId, lpTagetWndName);
+		}
+
+		return hTagetWnd;
+	}
+	
 	std::vector<GameHwnd> g_vtGameHwnd;
 	std::vector<GameHwnd> g_vtGameHwnd;
 	BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam)  
 	BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam)  
 	{  
 	{  

+ 7 - 0
GameAssist/GameAssist/Assist.h

@@ -34,6 +34,13 @@ namespace GAssist
 	extern HWND GetProMainHwnd(DWORD dwProcessId);
 	extern HWND GetProMainHwnd(DWORD dwProcessId);
 	extern BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam);
 	extern BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam);
 
 
+	// 根据进程名获取进程pid;
+	extern DWORD GetProcessId(LPCTSTR lpProcessName = _T("Game.exe"));
+	// 根据进程pid获取进程对应程序的窗口句柄;
+	extern HWND GetProcessWnd(const DWORD &dwTagetProcessId, LPCTSTR lpTagetWndName = _T("大话水浒"));
+	extern HWND GetProcessWnd(LPCTSTR lpProcessName = _T("Game.exe"), LPCTSTR lpTagetWndName = _T("大话水浒"));
+
+
 	typedef struct __GAME_HWNINFO__{
 	typedef struct __GAME_HWNINFO__{
 		DWORD			dwId;				// 控件id;
 		DWORD			dwId;				// 控件id;
 		std::string		strWinText;			// 控件文本;
 		std::string		strWinText;			// 控件文本;