Selaa lähdekoodia

完成监听进程窗口

sat23 3 vuotta sitten
vanhempi
commit
235292ee66

+ 321 - 0
StartAPPbyCOM/StartAPPbyCOM/Assist.cpp

@@ -0,0 +1,321 @@
+#include "Assist.h"
+
+
+namespace Assist
+{
+	int frameWidth = 2;//6
+	int titleBarHeight = 27;//29
+
+	TCHAR g_szCurModuleFileName[MAX_PATH];
+	TCHAR g_szCurModuleDir[MAX_PATH];
+	TCHAR g_szCurModulePath[MAX_PATH];
+	TCHAR g_szFna[_MAX_FNAME];
+	TCHAR g_szExt[_MAX_EXT];
+	TCHAR g_szAssistConfig[MAX_PATH];
+	std::string g_strAppdir;
+	std::string g_strGameDir;
+	TCHAR g_szGameApp[MAX_PATH];
+	int nGameWndType;
+	int nAttackCount = 0;
+
+	void Init()
+	{
+		TCHAR szDrive[_MAX_DRIVE] = { 0 };
+		TCHAR szDir[_MAX_DIR] = { 0 };
+		TCHAR szFna[_MAX_FNAME] = { 0 };
+		TCHAR szExt[_MAX_EXT] = { 0 };
+		DWORD dwRet = ::GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
+
+		_tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
+		_tcscat_s(g_szCurModuleDir, MAX_PATH, szDrive);
+		_tcscat_s(g_szCurModuleDir, MAX_PATH, szDir);
+
+		//g_strAppdir = g_szCurModuleDir;
+		_stprintf_s(g_szAssistConfig, _T("%s%s"), g_szCurModuleDir, _T("Assist.ini"));
+
+		TCHAR szValue[MAX_PATH] = { 0 };
+		GetPrivateProfileString(_T("Assist"), _T("GamePath"), _T(""), szValue, MAX_PATH, g_szAssistConfig);
+		//g_strGameDir = szValue;
+		GetPrivateProfileString(_T("Assist"), _T("GameApp"), _T(""), g_szGameApp, MAX_PATH, g_szAssistConfig);
+
+		// 读取窗口类型;
+		_stprintf_s(szValue, _T("%s%s"), g_szGameApp, _T("save\\config.ini"));
+		int ScreenWidth = GetPrivateProfileInt(_T("SysCfg"), _T("ScreenWidth"), 640, szValue);
+
+		// Alt+A次数;
+		nAttackCount = GetPrivateProfileInt(_T("SysCfg"), _T("AttackCount"), 120, szValue);
+	}
+
+	void SetWindowForeground(HWND hWnd)
+	{
+		if (!IsWindow(hWnd))
+			return;
+
+		::ShowWindow(hWnd, SW_SHOWNORMAL);
+		::SetForegroundWindow(hWnd); // 窗口前置才能单击成功;
+	}
+
+	bool IsWindowForeground(HWND hWnd)
+	{
+		return false;
+	}
+
+	bool StartApp(const TCHAR* szAppPath, const TCHAR* szAppArgs)
+	{
+		if (!PathFileExists(szAppPath))
+		{
+			printf("Error, %ls path not exists!\n", szAppPath);
+			return false;
+		}
+
+		SHELLEXECUTEINFO ShExecInfo;
+		ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
+		ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
+		ShExecInfo.hwnd = NULL;
+		ShExecInfo.lpVerb = _T("open");
+		ShExecInfo.lpFile = szAppPath;
+		ShExecInfo.lpParameters = szAppArgs;
+		ShExecInfo.lpDirectory = NULL;
+		ShExecInfo.nShow = SW_SHOW;
+		ShExecInfo.hInstApp = NULL;
+
+		if (ShellExecuteEx(&ShExecInfo))
+		{
+			CloseHandle(ShExecInfo.hProcess);
+			return true;
+		}
+
+		return false;
+	}
+
+	void CloseProcess(LPCTSTR lpProcessName)
+	{
+		PROCESSENTRY32	pe32 = { 0 };
+		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+		if (hProcessSnap == NULL) return;
+
+		// 遍历进程;
+		pe32.dwSize = sizeof(PROCESSENTRY32);
+		for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
+			if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
+				HANDLE hProc = ::OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
+				TerminateProcess(hProc, 4);
+			}
+		}
+
+		CloseHandle(hProcessSnap);
+	}
+
+	// 返回第一个找到的进程pid
+	DWORD GetProcessId(LPCTSTR lpProcessName)
+	{
+		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;
+	}
+
+	void GetAllProcessId(std::vector<DWORD>& vtPid, LPCTSTR lpProcessName)
+	{
+		vtPid.clear();
+		PROCESSENTRY32	pe32 = { 0 };
+
+		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+		if (hProcessSnap == NULL) return;
+
+		// 遍历进程;
+		pe32.dwSize = sizeof(PROCESSENTRY32);
+		for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
+			if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
+				vtPid.push_back(pe32.th32ProcessID);
+
+				GetClassNameWnd(pe32.th32ProcessID, _T("Intermediate D3D Window"));
+				GetWindowNameWnd(pe32.th32ProcessID, _T("Feishu Rooms"));
+			}
+		}
+
+		CloseHandle(hProcessSnap);
+	}
+
+	HWND GetProcessMainWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWndName)
+	{
+		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 GetProcessMainWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWndName)
+	{
+		HWND hTagetWnd = NULL;
+		DWORD dwProcessId = 0;
+		if ((dwProcessId = GetProcessId(lpProcessName)) != 0) {
+			hTagetWnd = GetProcessMainWnd(dwProcessId, lpTagetWndName);
+		}
+
+		return hTagetWnd;
+	}
+
+	HWND GetClassNameWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetClassName)
+	{
+		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 (lpTagetClassName == 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(szClassName, lpTagetClassName) != NULL) {
+							hTagetProcessWnd = hCurWnd;
+							break;
+						}
+					}
+				}
+			}
+		}
+
+		return hTagetProcessWnd;
+	}
+
+	HWND GetWindowNameWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWindowName)
+	{
+		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 (lpTagetWindowName == 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, lpTagetWindowName) != NULL) {
+							hTagetProcessWnd = hCurWnd;
+							break;
+						}
+					}
+				}
+			}
+		}
+
+		return hTagetProcessWnd;
+	}
+
+	HWND GetWindowNameWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWindowName)
+	{
+		HWND hTagWnd = NULL;
+		PROCESSENTRY32	pe32 = { 0 };
+		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+		if (hProcessSnap == NULL) return NULL;
+
+		// 遍历进程;
+		pe32.dwSize = sizeof(PROCESSENTRY32);
+		for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
+			if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
+				hTagWnd = GetWindowNameWnd(pe32.th32ProcessID, lpTagetWindowName);
+				if (hTagWnd != NULL)
+					break;
+			}
+		}
+
+		CloseHandle(hProcessSnap);
+
+		return hTagWnd;
+	}
+};

+ 64 - 0
StartAPPbyCOM/StartAPPbyCOM/Assist.h

@@ -0,0 +1,64 @@
+#ifndef __ASSIST__
+#define __ASSIST__
+
+#pragma once
+
+#include <map>
+#include <string>
+#include <vector>
+#include <tchar.h>
+#include <windows.h>
+
+#include <Shlwapi.h>
+#pragma comment(lib,"Shlwapi.lib") /*需要加上此行才可以正确link,VC6.0*/
+#include <tlhelp32.h>
+
+
+namespace Assist {
+	extern int titleBarHeight;
+	extern int frameWidth;
+
+	extern TCHAR g_szCurModuleFileName[MAX_PATH];
+	extern TCHAR g_szCurModuleDir[MAX_PATH];
+	extern TCHAR g_szCurModulePath[MAX_PATH];
+	extern TCHAR g_szFna[_MAX_FNAME];
+	extern TCHAR g_szExt[_MAX_EXT];
+	extern TCHAR g_szAssistConfig[MAX_PATH];
+	extern std::string g_strAppdir;
+	extern std::string g_strGameDir;
+	extern TCHAR g_szGameApp[MAX_PATH];
+
+	typedef struct __HWNINFO__ {
+		DWORD			dwId;				// 控件id;
+		std::string		strWinText;			// 控件文本;
+		std::string		strClassName;		// 控件类名;
+		HWND			hwnd;				// 控件句柄;
+	}WinInfo, * pWinInfo;
+
+	extern std::vector<WinInfo> g_vtWndInfo;
+
+	void Init();
+	// 设置窗口前置;
+	void SetWindowForeground(HWND hWnd);
+	// 判断窗口是否前置;
+	bool IsWindowForeground(HWND hWnd);
+	bool StartApp(const TCHAR* szAppPath, const TCHAR* szAppArgs = NULL);
+
+	void CloseProcess(LPCTSTR lpProcessName);
+	// 根据进程名获取进程pid;
+	DWORD GetProcessId(LPCTSTR lpProcessName );
+	void GetAllProcessId(std::vector<DWORD>& vtPid, LPCTSTR lpProcessName);
+	// 根据进程pid获取进程对应程序的窗口句柄;
+	HWND GetProcessMainWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWndName);
+	// 获取指定进程名的进程主窗口;
+	HWND GetProcessMainWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWndName);
+	// 根据进程pid获取进程类名对应的窗口;
+	HWND GetClassNameWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetClassName);
+	// 根据进程pid获取进程窗口名对应的窗口;
+	HWND GetWindowNameWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWindowName);
+
+	// 获取符合要求的窗口进程;
+	HWND GetWindowNameWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWindowName);
+};
+
+#endif // __ASSIST__

+ 5 - 0
StartAPPbyCOM/StartAPPbyCOM/SerialPortManager.cpp

@@ -187,6 +187,11 @@ int SerialPortManager::listenCOMandStartApp(const TCHAR* szAppPath, const TCHAR*
 	return ret;
 }
 
+int SerialPortManager::sendStartedApp()
+{
+	return writeData(cmd_send_started_app, sizeof(cmd_send_started_app));
+}
+
 int SerialPortManager::writeData(const char* szBuf, int size)
 {
 	int ret = 0;

+ 1 - 0
StartAPPbyCOM/StartAPPbyCOM/SerialPortManager.h

@@ -41,6 +41,7 @@ public:
 
 	virtual int listenCOMandStartApp(const TCHAR* szAppPath, const TCHAR* szAppArgs);
 
+	virtual int sendStartedApp();
 protected:
 	virtual void release();
 

+ 34 - 2
StartAPPbyCOM/StartAPPbyCOM/StartAPPbyCOM.cpp

@@ -7,6 +7,7 @@
 #include "CmdHelper.h"
 #include "SerialPortManager.h"
 
+#include "Assist.h"
 
 #ifdef _UNICODE
 #pragma comment(linker, "/subsystem:\"windows\" /entry:\"wmainCRTStartup\"")
@@ -18,10 +19,10 @@
 #define     TAG_CMD_START_APP   _T("-startApp")
 #define     TAG_ARG_START_APP   _T("-startAppArg")
 
-
 int _tmain(int argc, TCHAR* argv[])
 {
     int ret = 0;
+    Sleep(30000);
 
     CmdHelper<TCHAR> cmdHelper;
     if ((ret = cmdHelper.initialize(argc, argv)) != 0)
@@ -41,7 +42,6 @@ int _tmain(int argc, TCHAR* argv[])
 
     cmdHelper.getCmdValue(TAG_ARG_START_APP, tcsAppArgs);
 
-
     SerialPortManager spManager;
 
     if ((ret = spManager.initialize()) != 0)
@@ -50,6 +50,38 @@ int _tmain(int argc, TCHAR* argv[])
         return ret;
     }
 
+#if 1
+	// 判断是否有飞书进程;
+	HWND hWnd = Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms"));
+	if (hWnd == NULL)
+	{
+		// 结束所有飞书进程;
+		Assist::CloseProcess(_T("FeishuRooms.exe"));
+
+		// 判断是否有任务栏;
+        DWORD dwTickCount = GetTickCount();
+		while (FindWindow(_T("Shell_TrayWnd"), NULL) == NULL)
+		{
+			Sleep(500);
+            if ( GetTickCount() - dwTickCount > 20000 )
+                break;
+		}
+
+		// 启动飞书;
+		Assist::StartApp(tcsAppPath.c_str(), tcsAppArgs.c_str());
+        dwTickCount = GetTickCount();
+		while (Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms")) == NULL)
+		{
+			Sleep(500);
+			if (GetTickCount() - dwTickCount > 10000)
+				break;
+		}
+
+		// 发送串口消息;
+        spManager.sendStartedApp();
+	}
+#endif
+
     if ((ret = spManager.listenCOMandStartApp(tcsAppPath.c_str(), tcsAppArgs.c_str())) != 0)
     {
         printf("listenCOMandStartApp failed ret=%d\n", ret);

+ 4 - 0
StartAPPbyCOM/StartAPPbyCOM/StartAPPbyCOM.vcxproj

@@ -94,6 +94,7 @@
       <ConformanceMode>true</ConformanceMode>
       <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
       <AdditionalIncludeDirectories>$(ProjectDir)CSerialPort\include;$(ProjectDir)CSerialPort\src</AdditionalIncludeDirectories>
+      <LanguageStandard_C>stdc17</LanguageStandard_C>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
@@ -111,6 +112,7 @@
       <ConformanceMode>true</ConformanceMode>
       <AdditionalIncludeDirectories>$(ProjectDir)CSerialPort\include;$(ProjectDir)CSerialPort\src</AdditionalIncludeDirectories>
       <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+      <LanguageStandard_C>stdc17</LanguageStandard_C>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
@@ -155,6 +157,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="Assist.cpp" />
     <ClCompile Include="CSerialPort\src\SerialPort.cpp" />
     <ClCompile Include="CSerialPort\src\SerialPortBase.cpp" />
     <ClCompile Include="CSerialPort\src\SerialPortInfo.cpp" />
@@ -165,6 +168,7 @@
     <ClCompile Include="StartAPPbyCOM.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="Assist.h" />
     <ClInclude Include="CmdHelper.h" />
     <ClInclude Include="CSerialPort\include\CSerialPort\osplatformutil.h" />
     <ClInclude Include="CSerialPort\include\CSerialPort\SerialPort.h" />

+ 6 - 0
StartAPPbyCOM/StartAPPbyCOM/StartAPPbyCOM.vcxproj.filters

@@ -42,6 +42,9 @@
     <ClCompile Include="CSerialPort\src\SerialPortWinBase.cpp">
       <Filter>CSerialPort</Filter>
     </ClCompile>
+    <ClCompile Include="Assist.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="CmdHelper.h">
@@ -80,6 +83,9 @@
     <ClInclude Include="CSerialPort\include\CSerialPort\sigslot.h">
       <Filter>CSerialPort</Filter>
     </ClInclude>
+    <ClInclude Include="Assist.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="StartAPPbyCOM.rc">