Browse Source

1、初步完成CreateProcess调用Python脚本;

scbc.sat2 5 years ago
parent
commit
e6295ce253
2 changed files with 81 additions and 18 deletions
  1. 77 17
      RunPython/RunPython/RunPython.cpp
  2. 4 1
      RunPython/RunPython/RunPython.h

+ 77 - 17
RunPython/RunPython/RunPython.cpp

@@ -16,7 +16,6 @@ CWinApp theApp;
 using namespace std;
 
 BOOL Python27Dir(LPTSTR lpPython27Dir, int nBufferLen);
-BOOL RunPython(LPCTSTR lpScriptFile);
 
 int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 {
@@ -34,7 +33,9 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 		// TODO: 在此处为应用程序的行为编写代码。
 		//RunPython("E:\\bin\\ScbcCopyKey\\test.py"); // 加载失败,但改成test2.py后,就能加载成功
 		//RunPython("E:\\bin\\ScbcCopyKey\\test2.py");
-		RunPython("D:\\SAT\\runner\\btc_runner_se\\runner\\output\\ODF_NPI_RT2841\\20191119172310094\\192.168.1.119_5555\\cases\\RT_2841\\ODF_NPI_RT2841\\picture\\22.py");
+		//RunPython("D:\\SAT\\runner\\btc_runner_se\\runner\\output\\ODF_NPI_RT2841\\20191119172310094\\192.168.1.119_5555\\cases\\RT_2841\\ODF_NPI_RT2841\\picture\\22.py", NULL);
+		printf("\n\n\n\n=================================================================\n\n");
+		CallPython("D:\\SAT\\runner\\btc_runner_se\\runner\\output\\ODF_NPI_RT2841\\20191119172310094\\192.168.1.119_5555\\cases\\RT_2841\\ODF_NPI_RT2841\\picture\\22.py", NULL);
 	}
 
 	system("pause");
@@ -64,24 +65,24 @@ BOOL Python27Dir(LPTSTR lpPython27Dir, int nBufferLen)
 }
 
 // 运行Python脚本;
-BOOL RunPython(LPCTSTR lpScriptFile)
+RUNPYTHON_API int RunPython(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence)
 {
 	// 参数有效性判断;
 	if ( !lpScriptFile || !PathFileExists(lpScriptFile) )
-		return FALSE;
+		return -1;
 
 	// 初始化Python环境;
 	Py_Initialize();
 	if ( !Py_IsInitialized() )
-		return FALSE;
+		return -2;
 
 	// 解析脚本路径和脚本名称;
 	std::string scriptdir;
 	TCHAR szDrive[_MAX_DRIVE] = { 0 };
 	TCHAR szDir[_MAX_DIR] = { 0 };
 	TCHAR szExt[_MAX_EXT] = { 0 };
-	TCHAR szFilename[_MAX_FNAME] = {0};
-	TCHAR szScriptDir[MAX_PATH] = {0};
+	TCHAR szFilename[_MAX_FNAME] = { 0 };
+	TCHAR szScriptDir[MAX_PATH] = { 0 };
 	_tsplitpath_s(lpScriptFile, szDrive, szDir, szFilename, szExt);	
 	_stprintf_s(szScriptDir, _T("%s%s"), szDrive, szDir);
 	// 缓存一份路径;
@@ -95,25 +96,23 @@ BOOL RunPython(LPCTSTR lpScriptFile)
 	}
 	//szScriptDir[len-1] = '\0';
 
-	TCHAR szExecuteDir[MAX_PATH] = {0};
+	TCHAR szExecuteDir[MAX_PATH] = { 0 };
 	_stprintf_s(szExecuteDir, _T("sys.path.append(\"%s\")"), szScriptDir);
 	// 添加系统模块,并指定当前脚本路径为运行路径;
 	PyRun_SimpleString("import sys");
 	PyRun_SimpleString(szExecuteDir);
+	// 运行额外的语句,一般用于传递命令行参数;
+	if ( lpExtraSentence )
+		PyRun_SimpleString(lpExtraSentence);
 
 	// 注意:脚本名称尽量不要与系统目录其他py文件相同;
 	// 导入脚本,以脚本名称为模块名加载;
-#if 1
-	PyObject *pModuleName = PyString_FromString(szFilename);
-	PyObject *pModuleObj = PyImport_Import(pModuleName);
-#else
 	PyObject *pModuleObj = PyImport_ImportModule(szFilename);
-#endif
 	if ( !pModuleObj )
 	{
 		printf("=>加载模块失败\n");
 		Py_Finalize();
-		return FALSE;
+		return -3;
 	}
 	
 	// 获取脚本的主函数名称(规定所有脚本的主函数名为main)
@@ -123,7 +122,7 @@ BOOL RunPython(LPCTSTR lpScriptFile)
 	{
 		printf("=>加载函数失败\n");
 		Py_Finalize();
-		return FALSE;
+		return -4;
 	}
 	
 	// 执行main函数;
@@ -132,10 +131,71 @@ BOOL RunPython(LPCTSTR lpScriptFile)
 	{
 		printf("=>运行函数失败\n");
 		Py_Finalize();
-		return FALSE;
+		return -5;
 	}
 
 	Py_Finalize();
 
-	return TRUE;
+	return 0;
+}
+
+// lpScriptFile:脚本文件;
+// lpCommand:命令行参数;
+RUNPYTHON_API int CallPython(LPCTSTR lpScriptFile, LPCTSTR lpCommand)
+{
+	if ( !lpScriptFile || !PathFileExists(lpScriptFile) )
+	{
+		printf("参数无效\n");
+		return -1;
+	}
+
+	// 获取Python27进程目录;
+	TCHAR szPython27Dir[MAX_PATH] = {0};
+	TCHAR szPython27Exe[MAX_PATH] = {0};
+	if ( !Python27Dir(szPython27Dir, MAX_PATH) )
+	{
+		printf("获取Python27目录失败\n");
+		return -2;
+	}
+	_stprintf_s(szPython27Exe, _T("%spython.exe"), szPython27Dir);
+
+	STARTUPINFO si;
+	::memset(&si, 0 ,sizeof(si));
+	si.cb = sizeof(si);
+
+	PROCESS_INFORMATION pi;
+	::memset(&pi, 0 ,sizeof(pi));
+
+	TCHAR szCommandLine[MAX_PATH] = {0};
+	if ( lpCommand )
+		_stprintf_s(szCommandLine, _T("python -W ignore %s %s"), lpScriptFile, lpCommand);
+	else
+		_stprintf_s(szCommandLine, _T("python -W ignore %s"), lpScriptFile);
+
+	// 启动子进程;
+	if( !CreateProcess( 
+		szPython27Exe,			// No module name (use command line)
+		szCommandLine,			// Command line
+		NULL,					// Process handle not inheritable
+		NULL,					// Thread handle not inheritable
+		FALSE,					// Set handle inheritance to TRUE
+		0,						// No creation flags
+		NULL,					// Use parent's environment block
+		NULL,					// Use parent's starting directory 
+		&si,					// Pointer to STARTUPINFO structure
+		&pi )					// Pointer to PROCESS_INFORMATION structure
+		) 
+	{
+		printf( "CreateProcess failed (%d).\n", GetLastError() );
+		return -3;
+	}
+
+	// Wait until child process exits.
+	WaitForSingleObject( pi.hProcess, INFINITE );
+
+	// Close process and thread handles. 
+	CloseHandle( pi.hProcess );
+	CloseHandle( pi.hThread );
+
+	return 0;
 }

+ 4 - 1
RunPython/RunPython/RunPython.h

@@ -19,4 +19,7 @@ public:
 
 extern RUNPYTHON_API int nRunPython;
 
-RUNPYTHON_API int fnRunPython(void);
+// 直接C++运行Python脚本;
+RUNPYTHON_API int RunPython(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence);
+// 调用Python.exe来运行脚本;
+RUNPYTHON_API int CallPython(LPCTSTR lpScriptFile, LPCTSTR lpCommand);