|
@@ -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[])
|
|
|
|
|
|
|
|
|
|
|
|
- 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");
|
|
|
+
|
|
|
+ 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)
|
|
|
}
|
|
|
|
|
|
|
|
|
-BOOL RunPython(LPCTSTR lpScriptFile)
|
|
|
+RUNPYTHON_API int RunPython(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence)
|
|
|
{
|
|
|
|
|
|
if ( !lpScriptFile || !PathFileExists(lpScriptFile) )
|
|
|
- return FALSE;
|
|
|
+ return -1;
|
|
|
|
|
|
|
|
|
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)
|
|
|
}
|
|
|
|
|
|
|
|
|
- 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);
|
|
|
|
|
|
|
|
|
|
|
|
-#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;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -123,7 +122,7 @@ BOOL RunPython(LPCTSTR lpScriptFile)
|
|
|
{
|
|
|
printf("=>加载函数失败\n");
|
|
|
Py_Finalize();
|
|
|
- return FALSE;
|
|
|
+ return -4;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -132,10 +131,71 @@ BOOL RunPython(LPCTSTR lpScriptFile)
|
|
|
{
|
|
|
printf("=>运行函数失败\n");
|
|
|
Py_Finalize();
|
|
|
- return FALSE;
|
|
|
+ return -5;
|
|
|
}
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
- return TRUE;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+RUNPYTHON_API int CallPython(LPCTSTR lpScriptFile, LPCTSTR lpCommand)
|
|
|
+{
|
|
|
+ if ( !lpScriptFile || !PathFileExists(lpScriptFile) )
|
|
|
+ {
|
|
|
+ printf("参数无效\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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,
|
|
|
+ szCommandLine,
|
|
|
+ NULL,
|
|
|
+ NULL,
|
|
|
+ FALSE,
|
|
|
+ 0,
|
|
|
+ NULL,
|
|
|
+ NULL,
|
|
|
+ &si,
|
|
|
+ &pi )
|
|
|
+ )
|
|
|
+ {
|
|
|
+ printf( "CreateProcess failed (%d).\n", GetLastError() );
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ WaitForSingleObject( pi.hProcess, INFINITE );
|
|
|
+
|
|
|
+
|
|
|
+ CloseHandle( pi.hProcess );
|
|
|
+ CloseHandle( pi.hThread );
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|