Jelajahi Sumber

输出接口定义;

scbc.sat2 5 tahun lalu
induk
melakukan
fe10289d8a

+ 0 - 8
RunPython/RunPython.sln

@@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 10.00
 # Visual Studio 2008
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RunPython", "RunPython\RunPython.vcproj", "{92FC4676-AAFC-4568-93EE-849029E5381D}"
 EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test\test.vcproj", "{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}"
-EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Win32 = Debug|Win32
@@ -18,12 +16,6 @@ Global
 		{92FC4676-AAFC-4568-93EE-849029E5381D}.DeubgExe|Win32.Build.0 = DeubgExe|Win32
 		{92FC4676-AAFC-4568-93EE-849029E5381D}.Release|Win32.ActiveCfg = Release|Win32
 		{92FC4676-AAFC-4568-93EE-849029E5381D}.Release|Win32.Build.0 = Release|Win32
-		{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}.Debug|Win32.ActiveCfg = Debug|Win32
-		{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}.Debug|Win32.Build.0 = Debug|Win32
-		{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}.DeubgExe|Win32.ActiveCfg = Debug|Win32
-		{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}.DeubgExe|Win32.Build.0 = Debug|Win32
-		{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}.Release|Win32.ActiveCfg = Release|Win32
-		{332DAF3B-C481-4F4F-8628-FBF1095BE4C2}.Release|Win32.Build.0 = Release|Win32
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 88 - 0
RunPython/RunPython/RunPython.cpp

@@ -57,3 +57,91 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 
 	return nRetCode;
 }
+
+// 全局变量;
+std::vector<CScriptExecutor*> g_vtScriptExcutor;
+
+RUNPYTHON_API DWORD RunPyScript(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence, LPCTSTR lpScriptLog, BOOL bSubProcess)
+{
+	CScriptExecutor *pExcutor = new CScriptExecutor();
+	if ( pExcutor )
+	{
+		// 初始化脚本;
+		pExcutor->InitScript(lpScriptFile, lpScriptLog, lpExtraSentence, bSubProcess ? SUBPROCESS : EMBEDDED);
+		// 执行脚本;
+		if ( !pExcutor->StartScript() )
+		{
+			delete pExcutor;
+			pExcutor = NULL;
+			return -1;
+		}
+
+		// 压入全局变量;
+		g_vtScriptExcutor.push_back(pExcutor);
+
+		// 返回脚本Id;
+		return pExcutor->GetScriptId();
+	}
+
+	return -1;
+}
+
+RUNPYTHON_API bool StopPyScript(DWORD dwScriptId, BOOL bSubProcess)
+{
+	bool ret = false;
+	CScriptExecutor *pExcutor = NULL;
+	std::vector<CScriptExecutor*>::iterator it = g_vtScriptExcutor.begin();
+	for ( ; it != g_vtScriptExcutor.end(); it++ )
+	{
+		pExcutor = *it;
+		if ( pExcutor->GetScriptId() == dwScriptId )
+		{
+			ret = true;
+			pExcutor->StopScript();
+			break;
+		}
+	}
+
+	return ret;
+}
+
+RUNPYTHON_API bool IsPyScriptOver(DWORD dwScriptId, BOOL bSubProcess)
+{
+	bool ret = false;
+	CScriptExecutor *pExcutor = NULL;
+	std::vector<CScriptExecutor*>::iterator it = g_vtScriptExcutor.begin();
+	for ( ; it != g_vtScriptExcutor.end(); it++ )
+	{
+		pExcutor = *it;
+		if ( pExcutor->GetScriptId() == dwScriptId )
+		{
+			if ( ret = pExcutor->IsScriptOver() )
+			{
+				break;
+			}		
+		}
+	}
+
+	return ret;
+}
+
+RUNPYTHON_API void CleanPyScript(DWORD dwScriptId)
+{
+	CScriptExecutor *pExcutor = NULL;
+	std::vector<CScriptExecutor*>::iterator it = g_vtScriptExcutor.begin();
+	for ( ; it != g_vtScriptExcutor.end(); it++ )
+	{
+		pExcutor = *it;
+		if ( pExcutor->GetScriptId() == dwScriptId )
+		{
+			// 先停止脚本;
+			pExcutor->StopScript();
+
+			// 再清除;
+			delete pExcutor;
+			pExcutor = NULL;
+			g_vtScriptExcutor.erase(it);
+			break;
+		}
+	}
+}

+ 15 - 14
RunPython/RunPython/RunPython.h

@@ -11,19 +11,20 @@
 #endif
 
 // 此类是从 RunPython.dll 导出的
-class RUNPYTHON_API CRunPython {
-public:
-	CRunPython(void);
-	// TODO: 在此添加您的方法。
-};
+//class RUNPYTHON_API CRunPython {
+//public:
+//	CRunPython(void);
+//	// TODO: 在此添加您的方法。
+//};
+//
+//extern RUNPYTHON_API int nRunPython;
 
-extern RUNPYTHON_API int nRunPython;
+// 运行脚本;
+RUNPYTHON_API DWORD RunPyScript(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence, LPCTSTR lpScriptLog, BOOL bSubProcess);
+// 结束脚本;
+RUNPYTHON_API bool StopPyScript(DWORD dwScriptId, BOOL bSubProcess);
+// 脚本是否结束;
+RUNPYTHON_API bool IsPyScriptOver(DWORD dwScriptId, BOOL bSubProcess);
+// 清除脚本;
+RUNPYTHON_API void CleanPyScript(DWORD dwScriptId);
 
-// 直接C++运行Python脚本;
-RUNPYTHON_API int RunPython(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence);
-// 实现标准错误输出;
-RUNPYTHON_API int RunPythonEx(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence);
-// 调用Python.exe来运行脚本;
-RUNPYTHON_API int CallPython(LPCTSTR lpScriptFile, LPCTSTR lpCommand);
-// 调用Python.exe来运行脚本,同时重定向输出流;
-RUNPYTHON_API int CallPythonEx(LPCTSTR lpScriptFile, LPCTSTR lpCommand);

+ 2 - 0
RunPython/RunPython/RunPython.vcproj

@@ -88,6 +88,7 @@
 			/>
 			<Tool
 				Name="VCPostBuildEventTool"
+				CommandLine="copy $(TargetDir)$(TargetName).lib ..\..\SATService\SATService\$(TargetName).lib /y/a&#x0D;&#x0A;copy $(TargetName).h ..\..\SATService\SATService\$(TargetName).h /y/a"
 			/>
 		</Configuration>
 		<Configuration
@@ -164,6 +165,7 @@
 			/>
 			<Tool
 				Name="VCPostBuildEventTool"
+				CommandLine="copy $(TargetDir)$(TargetName).lib ..\..\SATService\SATService\$(TargetName).lib /y/a&#x0D;&#x0A;copy $(TargetName).h ..\..\SATService\SATService\$(TargetName).h /y/a"
 			/>
 		</Configuration>
 		<Configuration

+ 8 - 1
RunPython/RunPython/ScriptExecutor.cpp

@@ -27,6 +27,8 @@ CScriptExecutor::CScriptExecutor(void)
 
 	m_bKillProcess = FALSE;
 	m_bStopLogExport = FALSE;
+	m_dwThreadId = 0;
+	m_dwSubprocessId = 0;
 }
 
 CScriptExecutor::~CScriptExecutor(void)
@@ -413,6 +415,7 @@ int CScriptExecutor::RunScripProcess()
 		return -3;
 	}
 
+	m_dwSubprocessId = m_pi.dwProcessId;
 	// 等待进程完成退出.
 	WaitForSingleObject(m_pi.hProcess, INFINITE);
 	// 结束日志线程;
@@ -433,7 +436,7 @@ int CScriptExecutor::RunScripProcess()
 bool CScriptExecutor::StartWorkThread()
 {
 	// 创建线程;
-	m_hWorkThread = CreateThread(NULL, 0, _WorkerThread, this, 0, NULL);
+	m_hWorkThread = CreateThread(NULL, 0, _WorkerThread, this, 0, &m_dwThreadId);
 	if (!m_hWorkThread)
 	{
 		printf("Error:创建线程失败\n");
@@ -482,6 +485,8 @@ void CScriptExecutor::EndWorkThread()
 		CloseHandle(m_hWorkThread);
 		m_hWorkThread = NULL;
 	}
+
+	m_dwThreadId = m_dwSubprocessId = 0;
 }
 
 void CScriptExecutor::EndLogThread()
@@ -537,6 +542,8 @@ BOOL CScriptExecutor::EndSubprocess()
 		}
 	}
 
+	m_dwSubprocessId = 0;
+
 	return ret;
 }
 

+ 13 - 0
RunPython/RunPython/ScriptExecutor.h

@@ -42,6 +42,10 @@ protected:
 	CTime			m_tStartTime;
 	// 执行器结束时间;
 	CTime			m_tEndTime;
+	// 子进程ID;
+	DWORD			m_dwSubprocessId;
+	// 线程ID;
+	DWORD			m_dwThreadId;
 protected:
 	// 工作者线程函数;
 	static DWORD WINAPI _WorkerThread(LPVOID lpParam);
@@ -84,6 +88,15 @@ public:
 	void StopScript();
 	// 脚本是否结束;
 	bool IsScriptOver();
+	// 运行类型;
+	int GetRunType() const { return m_nRunType;}
+	// 获取脚本Id;
+	DWORD GetScriptId() const {
+		if ( m_nRunType == SUBPROCESS )
+			return m_dwSubprocessId;
+		else
+			return m_dwThreadId;
+	}
 };
 
 #endif // __SCRIPT_EXECUTOR__