Forráskód Böngészése

1、修复获取遥控设备死循环的问题;
2、打开进程后,需要等待3秒左右,才可以连接tcp服务;

Jeff 5 éve
szülő
commit
1033b08421

+ 33 - 2
IRControl/IRControl/IRControl.cpp

@@ -162,6 +162,7 @@ DWORD CIRControl::IsAppRunning(LPCTSTR lpszAppDir)
 
 bool CIRControl::CloseApp(DWORD dwAppId)
 {
+	m_dwCurAppId = GetProcessId();
 	if (dwAppId == 0)
 		dwAppId = m_dwCurAppId;
 
@@ -197,6 +198,36 @@ bool CIRControl::CloseApp(DWORD dwAppId)
 	return true;
 }
 
+DWORD CIRControl::GetProcessId(std::string strApp)
+{
+	DWORD dwProcessID = 0;
+	PROCESSENTRY32 pe32 = { 0 };
+
+	HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+	if (hProcessSnap == NULL)
+	{
+		Global::WriteTextLog(_T("获取进程快照失败:%ld"), GetLastError());
+		return 0;
+	}
+	pe32.dwSize = sizeof(PROCESSENTRY32);
+
+	if (Process32First(hProcessSnap, &pe32))
+	{
+		do
+		{
+			// szExeFile只是文件名,不知道是否只有win10才这样;
+			if (_tcsicmp(strApp.c_str(), pe32.szExeFile) == 0)
+			{
+				m_dwCurAppId = dwProcessID = pe32.th32ProcessID;
+				break;
+			}
+		} while (Process32Next(hProcessSnap, &pe32));
+	}
+	CloseHandle(hProcessSnap);
+
+	return dwProcessID;
+}
+
 bool CIRControl::getDeviceNameList()
 {
 	return getDeviceNameList(m_vtDevices);
@@ -239,9 +270,9 @@ bool CIRControl::getDeviceNameList(std::vector<std::string>& vtDevice)
 
 		// 去除方括号;
 		fun_dev(vtDevice, data.substr(0, npos));
-		data = data.substr(npos);
+		data = data.substr(npos+1);
 	}
-	fun_dev(vtDevice, data.substr(0, npos));
+	fun_dev(vtDevice, data);
 
 	// 第一个设备给当前设备;
 	if (vtDevice.size())

+ 1 - 0
IRControl/IRControl/IRControl.h

@@ -36,6 +36,7 @@ public:
 	static bool StartApp(LPCTSTR lpAppDir, LPCTSTR lpSignalXml, DWORD dwPort = 40000);
 	static DWORD IsAppRunning(LPCTSTR lpszAppDir);
 	static bool CloseApp(DWORD dwAppId = 0);
+	static DWORD GetProcessId(std::string strApp = "RedRatHubCmd.exe");
 
 	// 红老鼠接口;
 public:

+ 7 - 0
IRControl/test/stdafx.cpp

@@ -119,6 +119,13 @@ void FreeLogLibarary()
 			g_IsAppRunning = NULL;
 			g_StartIRApp = NULL;
 			g_CloseApp = NULL;
+			g_Connect = NULL;
+			g_getDeviceNames = NULL;
+			g_getSignalsName = NULL;
+			g_loadSignalDataSet = NULL;
+			g_sendSignal = NULL;
+			g_sendSignals = NULL;
+			g_sendRepeatsSignal = NULL;
 		}
 	}
 }

+ 3 - 2
IRControl/test/test.cpp

@@ -171,7 +171,7 @@ int main()
         }
         else
         {
-#if 1 // 验证logModule多线程下写日志是否线程安全;
+#if 0 // 验证logModule多线程下写日志是否线程安全;
 			for ( int i = 0; i < 20; i++ )
 			{
 				// 创建10个线程;
@@ -201,6 +201,7 @@ int main()
 #endif
 			if (LoadLogLibarary())
 			{
+				g_CloseApp();
 				// 启动服务;
 				if (g_IsAppRunning(_T("C:\\Python27\\Tools\\RedRatHub-V4.28\\RedRatHubCmd.exe")) )
 					TRACE("启动红老鼠进程已运行\r");
@@ -217,7 +218,7 @@ int main()
 					}
 				}
 
-				Sleep(1000); // 等待进程启动完全;
+				Sleep(5000); // 等待进程启动完全;
 				// 连接服务器;
 				if (!g_Connect(_T("127.0.0.1"), 40000))
 					return 0;