Browse Source

Z991239-5240 #comment sprestart 程序的编译

80374374 1 year ago
parent
commit
95aa4a5f3f
3 changed files with 96 additions and 99 deletions
  1. 6 8
      Tool/CMakeLists.txt
  2. 27 0
      Tool/sprestart/CMakeLists.txt
  3. 63 91
      Tool/sprestart/sprestart.c

+ 6 - 8
Tool/CMakeLists.txt

@@ -30,12 +30,10 @@ SET(RVC_FRAMEWORK_INCLUDES_DIR ${RVC_COMMON_INCLUDE_DIR}
 
 ######################## Framework lib CMake define end ########################
 
-file(GLOB all_valid_subdirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*/CMakeLists.txt")
+add_subdirectory(GuardianBase)
+add_subdirectory(guardian)
+
+if(MSVC)
+    add_subdirectory(sprestart)
+endif(MSVC)
 
-foreach(dir ${all_valid_subdirs})
-	if(${dir} MATCHES "^([^/]*)/+CMakeLists.txt")
-		string(REGEX REPLACE "^([^/]*)/+CMakeLists.txt" "\\1" dir_trimmed ${dir})
-		message(STATUS "Adding other component ${dir_trimmed}")
-		add_subdirectory(${dir_trimmed})
-	endif()
-endforeach(dir)

+ 27 - 0
Tool/sprestart/CMakeLists.txt

@@ -0,0 +1,27 @@
+set(MODULE_NAME "sprestart")
+
+set(CMAKE_CXX_STANDARD 11)
+if(NOT MSVC)
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
+endif(NOT MSVC)
+
+add_executable(${MODULE_NAME} sprestart.c)
+
+target_include_directories(${MODULE_NAME} PRIVATE
+    ${RVC_TOOLKIT_INCLUDE_DIR}
+    ${RVC_WINPR_INCLUDE_DIR}
+)
+
+target_link_directories(${MODULE_NAME} PRIVATE
+    ${RVC_FRAMEWORK_LIBRARIES_DIR}
+)
+
+target_link_libraries(${MODULE_NAME} ${WINPR_LIB} ${TOOLKIT_LIB} Ws2_32)
+
+install(TARGETS ${MODULE_NAME} 
+    RUNTIME DESTINATION "${RVC_RUNTIME_PATH}"
+    ARCHIVE DESTINATION "${RVC_LIBRARY_PATH}"
+    LIBRARY DESTINATION "${RVC_LIBRARY_PATH}"
+	COMPONENT libraries)
+
+set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "tools/")

+ 63 - 91
Tool/sprestart/sprestart.c

@@ -1,71 +1,68 @@
-#include "precompile.h"
+#include <WinSock2.h>
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <Mmsystem.h>
+#include <windows.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <process.h>
+#include <errno.h>
+#include <time.h>
+#include <string.h>
+#include <crtdbg.h>
+#include <locale.h>
+#include <time.h>
 #include <TlHelp32.h>
 
 BOOL bSpshellKilled = FALSE;
-BOOL CALLBACK spEnumWindowsProc(HWND hwnd,LPARAM lParam)
+BOOL CALLBACK spEnumWindowsProc(HWND hwnd, LPARAM lParam)
 {
-	char title[300] = {0};
-	SendMessage(hwnd,WM_GETTEXT,256,(LPARAM)title);
+	char title[300] = { 0 };
+	SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)title);
 	if (_stricmp(title, "Microsoft Visual C++ Runtime Library") == 0)
-		SendMessage(hwnd, WM_COMMAND,((WPARAM)BN_CLICKED)<<16|(WPARAM)3,0L);
+		SendMessage(hwnd, WM_COMMAND, ((WPARAM)BN_CLICKED) << 16 | (WPARAM)3, 0L);
 	return TRUE;
 }
 int KillProc()
 {
-	
+
 	HANDLE hSnapshot;
-	
+
 	int rc = TRUE;
-	int result,xenum ;
+	int result, xenum;
 
-	char xx1[64] = {0};
-	char xx[64] = {0};
-	char xx2[64] = {0};
-	char xx3[64] = {0};
+	char xx1[64] = { 0 };
+	char xx[64] = { 0 };
+	char xx2[64] = { 0 };
+	char xx3[64] = { 0 };
 	HANDLE hProcess;
 	BOOL bFindSpshell = FALSE;
 	//find and kill spshell.exe
 	hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
-	if (hSnapshot) 
-	{
-		
+	if (hSnapshot) {
 		PROCESSENTRY32 pe;
 		pe.dwSize = sizeof(pe);
-		
-		if (Process32First(hSnapshot, &pe)) 
-		{
+
+		if (Process32First(hSnapshot, &pe)) {
 			do {
-				if (_stricmp(&pe.szExeFile[0], "spshell.exe") == 0) 
-				{
+				if (_stricmp(&pe.szExeFile[0], "spshell.exe") == 0) {
 					bFindSpshell = TRUE;
-					hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID );
-					if( hProcess == NULL )
-					{
-						//MessageBoxA(0,"Fail to open process!",0,0);
-						//MessageBoxA(0,_itoa((int)GetLastError(),xx3,10),0,0);
-						//return -1;
+					hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
+					if (hProcess == NULL) {
 						continue;
-					}
-					else
-					{
-						result = TerminateProcess(hProcess,-1);
+					} else {
+						result = TerminateProcess(hProcess, -1);
 						if (result)
 							bSpshellKilled = TRUE;
-						else
-						{
-							//MessageBoxA(0,"Terminate False",0,0);
-							//MessageBoxA(0,_itoa((int)GetLastError(),xx,10),0,0);
-							//return -1;
+						else {
 							continue;
 						}
-						WaitForSingleObject(hProcess, INFINITE );
-						CloseHandle( hProcess );
+						WaitForSingleObject(hProcess, INFINITE);
+						CloseHandle(hProcess);
 					}
-
-					
-					
-					xenum = EnumWindows(spEnumWindowsProc,0);
-
+					xenum = EnumWindows(spEnumWindowsProc, 0);
 					break;
 				}
 			} while (Process32Next(hSnapshot, &pe));
@@ -74,42 +71,28 @@ int KillProc()
 		}
 		CloseHandle(hSnapshot);
 	}
+
 	//find and kill sphost.exe if any
 	hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
-	if (hSnapshot)
-	{
+	if (hSnapshot) {
 		PROCESSENTRY32 pe;
 		pe.dwSize = sizeof(pe);
 
-		if (Process32First(hSnapshot, &pe))
-		{
+		if (Process32First(hSnapshot, &pe)) {
 			do {
-				if (_stricmp(&pe.szExeFile[0], "sphost.exe") == 0) 
-				{
-					hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID );
-					if( hProcess == NULL )
-					{
-						//MessageBoxA(0,"Fail to open process(2)!",0,0);
-						//MessageBoxA(0,_itoa((int)GetLastError(),xx3,10),0,0);
-						//return -1;
+				if (_stricmp(&pe.szExeFile[0], "sphost.exe") == 0) {
+					hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
+					if (hProcess == NULL) {
 						continue;
-					}
-					else
-					{
-						result = TerminateProcess(hProcess,-1);
-						if (!result)
-						{
-							//MessageBoxA(0,"False(2)",0,0);
-							//MessageBoxA(0,_itoa((int)GetLastError(),xx,10),0,0);
-							//return -1;
+					} else {
+						result = TerminateProcess(hProcess, -1);
+						if (!result) {
 							continue;
 						}
-						WaitForSingleObject(hProcess, INFINITE );
-						CloseHandle( hProcess );
+						WaitForSingleObject(hProcess, INFINITE);
+						CloseHandle(hProcess);
 					}
-
-
-					xenum = EnumWindows(spEnumWindowsProc,0);
+					xenum = EnumWindows(spEnumWindowsProc, 0);
 				}
 			} while (Process32Next(hSnapshot, &pe));
 		}
@@ -117,18 +100,17 @@ int KillProc()
 	}
 	return 0;
 }
+
 int RestartProc(const char* csPath)
 {
 
 	STARTUPINFO si;
 	PROCESS_INFORMATION pi;
-	if (bSpshellKilled)
-	{
-		//MessageBoxA(0,(LPCSTR)csPath,0,0);
-		ZeroMemory( &si, sizeof(si) );
+	if (bSpshellKilled) {
+		ZeroMemory(&si, sizeof(si));
 		si.cb = sizeof(si);
-		ZeroMemory( &pi, sizeof(pi) );
-		if( !CreateProcess( NULL,
+		ZeroMemory(&pi, sizeof(pi));
+		if (!CreateProcess(NULL,
 			(LPSTR)csPath,
 			NULL,
 			NULL,
@@ -137,16 +119,15 @@ int RestartProc(const char* csPath)
 			NULL,
 			NULL,
 			&si,
-			&pi )
-			) 
-		{
-			printf( "CreateProcess failed (%d).\n", GetLastError() );
+			&pi)
+			) {
+			printf("CreateProcess failed (%d).\n", GetLastError());
 			return -1;
 		}
 	}
 	return 0;
 }
-int main(int argc, char **argv)
+int main(int argc, char** argv)
 {
 	//find and kill the process
 	//
@@ -155,23 +136,14 @@ int main(int argc, char **argv)
 	WinExec("cmd.exe /c taskkill  /f /im spshell.exe", SW_HIDE);
 	WinExec("cmd.exe /c taskkill  /f /im sphost.exe", SW_HIDE);
 	Sleep(2000);
-// 	system("taskkill  /f /im spshell.exe");
-// 	system("taskkill  /f /im sphost.exe");
- 	bSpshellKilled = 1;
-/*	retKill = KillProc();*/
-	//if have killed spshell.exe,restart it
-	
-	//MessageBoxA(0,(LPCSTR)argv[0],0,0);
-	//MessageBoxA(0,(LPCSTR)argv[1],0,0);
-	//MessageBoxA(0,(LPCSTR)argv[2],0,0);
-	if (_strnicmp(argv[2],"r",1) == 0)
-	{
+	bSpshellKilled = 1;
+	if (_strnicmp(argv[2], "r", 1) == 0) {
 		RestartProc(argv[1]);
 	}
 	return 0;
 }
 
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
 {
 	return main(__argc, __argv);
 }