Bläddra i källkod

!1 添加启动关闭软件和打开脚本功能

80374463 6 år sedan
förälder
incheckning
fe57cf3eeb
5 ändrade filer med 166 tillägg och 0 borttagningar
  1. 2 0
      spbase/SpBase.vcxproj
  2. 56 0
      spbase/sp_cfg.cpp
  3. 86 0
      spbase/sp_runTask.cpp
  4. 18 0
      spbase/sp_runTask.h
  5. 4 0
      spshell/app.cpp

+ 2 - 0
spbase/SpBase.vcxproj

@@ -144,6 +144,7 @@
     <ClCompile Include="sp_pst.c" />
     <ClCompile Include="sp_rpc.c" />
     <ClCompile Include="sp_rsn.c" />
+    <ClCompile Include="sp_runTask.cpp" />
     <ClCompile Include="sp_ses.c" />
     <ClCompile Include="sp_shm.c" />
     <ClCompile Include="sp_sps.c" />
@@ -205,6 +206,7 @@
     <ClInclude Include="sp_pst.h" />
     <ClInclude Include="sp_rpc.h" />
     <ClInclude Include="sp_rsn.h" />
+    <ClInclude Include="sp_runTask.h" />
     <ClInclude Include="sp_ses.h" />
     <ClInclude Include="sp_shm.h" />
     <ClInclude Include="sp_sps.h" />

+ 56 - 0
spbase/sp_cfg.cpp

@@ -17,6 +17,8 @@
 #include "CodeSignVerify.h"
 #include <fstream>
 #include <ostream>
+#include <string>
+#include <vector>
 
 
 #define MAX_ENTITY_LEN	33
@@ -36,6 +38,17 @@
 #define T_ARRAY_MAKE(n, size, shm) \
 	shm ? shm_array_make(n, size) : array_make(n, size)
 
+std::vector<std::string> g_arrKill;
+std::vector<std::string> g_arrStart;
+
+std::vector<std::string> getKillArr() {
+	return g_arrKill; 
+}
+
+std::vector<std::string> getStartArr() {
+	return g_arrStart;
+}
+
 static int verify_entity_name(const char *name)
 {
 	const char *p = name;
@@ -872,9 +885,52 @@ static int load_shell_ini(sp_dir_t *dir, sp_cfg_shell_ini_t *shell, sp_cfg_root_
 		shell->shell_debug_level = 0;
 
 	shell->nConsolePort = inifile_read_int(file, "Main", "ConsolePort", 0);
+
+	auto killNum = inifile_read_int(file, "killProcess", "Number", 0);
+	g_arrKill.clear();
+	for (int i = 1; i <= killNum; i++)
+	{
+		char key[512];
+		char* s;
+		_itoa(i, key, 10);
+		s = inifile_read_str(file, "killProcess", key, "");
+		if (s && strlen(s) > 0) {
+			g_arrKill.push_back(s);
+			toolkit_free(s);
+		}
+		else
+		{
+			sp_dbg_warn("read kill failed! killProcess list [%s] define invalid!", key);
+			rc = Error_Param;
+			break;
+		}
+	}
+
+	auto startNum = inifile_read_int(file, "RunScript", "Number", 0);
+	g_arrStart.clear();
+	for (int i = 1; i <= startNum; i++)
+	{
+		char key[512];
+		char* s;
+		_itoa(i, key, 10);
+		s = inifile_read_str(file, "RunScript", key, "");
+		if (s && strlen(s) > 0) {
+			std::string fullPath = std::string(dir->bin_path) + "\\spScript\\" + s;
+			g_arrStart.push_back(fullPath);
+			toolkit_free(s);
+		}
+		else
+		{
+			sp_dbg_warn("read start failed! RunScript list [%s] define invalid!", key);
+			rc = Error_Param;
+			break;
+		}
+	}
+
 	return rc;
 }
 
+
 static int unload_shell_ini(sp_cfg_shell_ini_t *shell)
 {
 	int i;

+ 86 - 0
spbase/sp_runTask.cpp

@@ -0,0 +1,86 @@
+#include "precompile.h"
+#include "sp_runTask.h"
+#include <string>
+#include <vector>
+#include "sp_dir.h"
+#include "sp_dbg_export.h"
+#include <Windows.h>
+#include <TlHelp32.h>
+
+extern std::vector<std::string> getKillArr();
+
+extern std::vector<std::string> getStartArr();
+
+HANDLE FindProcessByName(const char* pProcessName)
+{
+	PROCESSENTRY32 pe32;
+	pe32.dwSize = sizeof(pe32);
+
+	HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+	BOOL bMore = ::Process32First(hProcessSnap, &pe32);
+	while (bMore)
+	{
+		if (stricmp(pProcessName, pe32.szExeFile) == 0)
+		{
+			return OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
+		}
+		bMore = ::Process32Next(hProcessSnap, &pe32);
+	}
+
+	return NULL;
+}
+
+int sp_runtask_killprocess()
+{
+	auto killArr = getKillArr();
+	for each (auto it in killArr)
+	{
+		char cmdStr[MAX_PATH] = "";
+		sprintf(cmdStr, "taskkill /f /im %s", it.c_str());
+		sp_dbg_debug("run %s", cmdStr);
+		WinExec(cmdStr, SW_HIDE);
+		int i = 0;
+		for (i = 0; i < 20; i++)
+		{
+			if (NULL == FindProcessByName(it.c_str()))
+				break;
+			Sleep(100);
+		}
+		if(i == 20)
+			sp_dbg_warn("kill %s failed", it.c_str());
+		
+	}
+	Sleep(1000);
+	return 0;
+}
+
+bool checkFileExist(std::string fileName)
+{
+	WIN32_FIND_DATA FindFileData;
+	HANDLE hFind;
+
+
+	hFind = FindFirstFile(fileName.c_str(), &FindFileData);
+
+	if (hFind == INVALID_HANDLE_VALUE)
+		return false;
+
+	FindClose(hFind);
+	return true;
+}
+
+int sp_runtask_startprocess()
+{
+	auto startArr = getStartArr();
+	for each (auto it in startArr)
+	{
+		if (!checkFileExist(it))
+		{
+			sp_dbg_debug("%s not exist", it.c_str());
+			return 0;
+		}
+		sp_dbg_debug("run %s", it.c_str());
+		WinExec(it.c_str(), SW_HIDE);
+	}
+	return 0;
+}

+ 18 - 0
spbase/sp_runTask.h

@@ -0,0 +1,18 @@
+#ifndef SP_RUNTASK_H
+#define SP_RUNTASK_H
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+	SPBASE_API int sp_runtask_killprocess();
+	SPBASE_API int sp_runtask_startprocess();
+
+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+#endif // SP_RUNTASK_H

+ 4 - 0
spshell/app.cpp

@@ -118,6 +118,7 @@ static int kickoff_startlist()
 }
 
 #include "shm_mem.h"
+#include "sp_runTask.h"
 
 int  RenameLightPacks(const char *pszAdPath)
 {
@@ -267,6 +268,8 @@ int app_init()
 
 	sp_pst_recover(env->dir->obj_path);
 
+	sp_runtask_killprocess();
+
 	urls[0] = env->url;
 #if 1
 	rc = bus_daemon_create(array_size(urls), urls, 2, &g_app.bus_daemon);
@@ -388,6 +391,7 @@ int app_init()
 	}
 #endif
 
+	sp_runtask_startprocess();
 	// close spshell dbg info
 	// sp_dbg_set_output_gui(NULL);