|
@@ -45,529 +45,6 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
|
|
|
}
|
|
|
|
|
|
system("pause");
|
|
|
- return nRetCode;
|
|
|
-}
|
|
|
-
|
|
|
-void CatchPythonException()
|
|
|
-{
|
|
|
- if ( !Py_IsInitialized() )
|
|
|
- {
|
|
|
- printf("未初始化Python环境\n");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if ( !PyErr_Occurred() )
|
|
|
- {
|
|
|
- printf("没有异常产生\n");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- const char *pszErrMsg = NULL;
|
|
|
- TCHAR *pszTraceback = NULL;
|
|
|
-
|
|
|
- PyObject *pPyType = NULL;
|
|
|
- PyObject *pPyValue = NULL;
|
|
|
- PyObject *pPyTraceback = NULL;
|
|
|
-
|
|
|
- PyErr_Fetch(&pPyType, &pPyValue, &pPyTraceback);
|
|
|
- PyErr_NormalizeException(&pPyType, &pPyValue, &pPyTraceback);
|
|
|
- if ( pPyValue )
|
|
|
- {
|
|
|
- PyObject *pPyStr = PyObject_Str(pPyValue);
|
|
|
- if ( PyString_Check(pPyStr) )
|
|
|
- {
|
|
|
- pszErrMsg = PyString_AsString(pPyStr);
|
|
|
- if ( pszErrMsg )
|
|
|
- printf("Error Info=>%s\n",pszErrMsg);
|
|
|
-
|
|
|
- if ( pPyTraceback )
|
|
|
- {
|
|
|
- PyObject *pPyTraceModule = PyImport_ImportModule("traceback");
|
|
|
- if ( !pPyTraceModule )
|
|
|
- {
|
|
|
- printf("导入traceback模块失败\n");
|
|
|
- return;
|
|
|
- }
|
|
|
-#if 1
|
|
|
- PyObject *pPyModuleDict = PyModule_GetDict(pPyTraceModule);
|
|
|
- if ( pPyModuleDict )
|
|
|
- {
|
|
|
- PyObject *pPyFunc = PyDict_GetItemString(pPyModuleDict, "format_exception");
|
|
|
- if ( !pPyFunc || !PyCallable_Check(pPyFunc) )
|
|
|
- {
|
|
|
- printf("加载format_exception失败\n");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- PyObject *pErroList = PyObject_CallFunctionObjArgs(pPyFunc, pPyType, pPyValue, pPyTraceback, NULL);
|
|
|
- if ( pErroList )
|
|
|
- {
|
|
|
- int nSize = PyList_Size(pErroList);
|
|
|
- for ( int i = 0; i < nSize; i++ )
|
|
|
- {
|
|
|
- pszErrMsg = PyString_AsString(PyList_GetItem(pErroList, i));
|
|
|
- printf("%s", pszErrMsg);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Py_XDECREF(pPyTraceModule);
|
|
|
-#else
|
|
|
- PyObject *pPyFunc = PyObject_GetAttrString(pPyTraceModule, "format_exception");
|
|
|
- if ( !pPyFunc || !PyCallable_Check(pPyFunc) )
|
|
|
- {
|
|
|
- printf("加载format_exception失败\n");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- PyObject *pPyResult = PyObject_CallFunctionObjArgs(pPyFunc, pPyType, pPyValue, pPyTraceback, NULL);
|
|
|
- pPyStr = PyObject_Str(pPyResult);
|
|
|
- pszErrMsg = PyString_AsString(pPyStr);
|
|
|
- if ( pszErrMsg )
|
|
|
- printf("%s\n",pszErrMsg);
|
|
|
-
|
|
|
- Py_DECREF(pPyResult);
|
|
|
- Py_XDECREF(pPyTraceModule);
|
|
|
-#endif
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-BOOL Python27Dir(LPTSTR lpPython27Dir, int nBufferLen)
|
|
|
-{
|
|
|
- HKEY hKey;
|
|
|
- int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
|
|
|
- if (ret != ERROR_SUCCESS)
|
|
|
- return FALSE;
|
|
|
-
|
|
|
-
|
|
|
- DWORD dwType = REG_SZ;
|
|
|
- DWORD cbData = nBufferLen;
|
|
|
- ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)lpPython27Dir, &cbData);
|
|
|
- if (ret != ERROR_SUCCESS)
|
|
|
- {
|
|
|
- RegCloseKey(hKey);
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- RegCloseKey(hKey);
|
|
|
-
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-RUNPYTHON_API int RunPython(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence)
|
|
|
-{
|
|
|
-
|
|
|
- if ( !lpScriptFile || !PathFileExists(lpScriptFile) )
|
|
|
- return -1;
|
|
|
-
|
|
|
-
|
|
|
- Py_Initialize();
|
|
|
- if ( !Py_IsInitialized() )
|
|
|
- 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 };
|
|
|
- _tsplitpath_s(lpScriptFile, szDrive, szDir, szFilename, szExt);
|
|
|
- _stprintf_s(szScriptDir, _T("%s%s"), szDrive, szDir);
|
|
|
-
|
|
|
- scriptdir = szScriptDir;
|
|
|
-
|
|
|
- int len = _tcslen(szScriptDir);
|
|
|
- for ( int i = 0; i < len; i++ )
|
|
|
- {
|
|
|
- if ( szScriptDir[i] == '\\' )
|
|
|
- szScriptDir[i] = '/';
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- 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);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- PyObject *pModuleObj = PyImport_ImportModule(szFilename);
|
|
|
- if ( !pModuleObj )
|
|
|
- {
|
|
|
- printf("=>加载模块失败\n");
|
|
|
- Py_Finalize();
|
|
|
- return -3;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- PyObject *pFunction = PyObject_GetAttrString(pModuleObj, "main");
|
|
|
- if ( !pFunction || !PyCallable_Check(pFunction) )
|
|
|
- {
|
|
|
- printf("=>加载函数失败\n");
|
|
|
- Py_Finalize();
|
|
|
- return -4;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- PyObject *pResult = PyObject_CallObject(pFunction, NULL);
|
|
|
- if ( !pResult )
|
|
|
- {
|
|
|
- printf("=>运行函数失败\n");
|
|
|
- Py_Finalize();
|
|
|
- return -5;
|
|
|
- }
|
|
|
-
|
|
|
- Py_Finalize();
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-RUNPYTHON_API int RunPythonEx(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence)
|
|
|
-{
|
|
|
-
|
|
|
- if ( !lpScriptFile || !PathFileExists(lpScriptFile) )
|
|
|
- return -1;
|
|
|
-
|
|
|
-
|
|
|
- Py_Initialize();
|
|
|
- if ( !Py_IsInitialized() )
|
|
|
- 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 };
|
|
|
- _tsplitpath_s(lpScriptFile, szDrive, szDir, szFilename, szExt);
|
|
|
- _stprintf_s(szScriptDir, _T("%s%s"), szDrive, szDir);
|
|
|
-
|
|
|
- scriptdir = szScriptDir;
|
|
|
-
|
|
|
- int len = _tcslen(szScriptDir);
|
|
|
- for ( int i = 0; i < len; i++ )
|
|
|
- {
|
|
|
- if ( szScriptDir[i] == '\\' )
|
|
|
- szScriptDir[i] = '/';
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- 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);
|
|
|
-
|
|
|
-
|
|
|
- PyObject* main = PyModule_GetDict(PyImport_AddModule("__main__"));
|
|
|
- PyObject *res = PyRun_String(由文件内容, Py_file_input, main, main);
|
|
|
- */
|
|
|
-
|
|
|
-
|
|
|
- PyObject *pModuleObj = PyImport_ImportModule(szFilename);
|
|
|
- if ( !pModuleObj )
|
|
|
- {
|
|
|
- printf("=>加载模块失败\n");
|
|
|
- Py_Finalize();
|
|
|
- return -3;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- PyObject *pFunction = PyObject_GetAttrString(pModuleObj, "main");
|
|
|
- if ( !pFunction || !PyCallable_Check(pFunction) )
|
|
|
- {
|
|
|
- printf("=>加载函数失败\n");
|
|
|
- Py_Finalize();
|
|
|
- return -4;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- PyObject *pResult = PyObject_CallObject(pFunction, NULL);
|
|
|
- if ( !pResult )
|
|
|
- {
|
|
|
- printf("=>运行函数失败\n");
|
|
|
-#if 0
|
|
|
-
|
|
|
- const char *pszErrMsg = NULL;
|
|
|
- TCHAR *pszTraceback = NULL;
|
|
|
-
|
|
|
- PyObject *pType = NULL;
|
|
|
- PyObject *pValue = NULL;
|
|
|
- PyObject *pTraceback = NULL;
|
|
|
-
|
|
|
- PyErr_Fetch(&pType, &pValue, &pTraceback);
|
|
|
- PyErr_NormalizeException(&pType,&pValue,&pTraceback);
|
|
|
- if ( pValue )
|
|
|
- {
|
|
|
- PyObject *pStr = PyObject_Str(pValue);
|
|
|
- if ( pStr )
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- int line, offset;
|
|
|
- TCHAR *pszMsg, *pszFile, *pszText;
|
|
|
- int res = PyArg_ParseTuple(pValue, "s(siis)", &pszMsg, &pszFile, &line, &offset, &pszText);
|
|
|
-
|
|
|
-
|
|
|
- PyObject *line_no = PyObject_GetAttrString(pValue, "lineno");
|
|
|
- PyObject *line_no_str = PyObject_Str(line_no);
|
|
|
- PyObject *line_no_unicode = PyUnicode_AsEncodedString(line_no_str, "utf-8", "Error");
|
|
|
-
|
|
|
- char *actual_line_no = PyBytes_AsString(line_no_unicode);
|
|
|
- printf("actual_line_no --%s\n", actual_line_no);
|
|
|
- }
|
|
|
- }
|
|
|
-#else
|
|
|
- CatchPythonException();
|
|
|
-#endif
|
|
|
-
|
|
|
- PyErr_Print();
|
|
|
- Py_Finalize();
|
|
|
- return -5;
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- Py_DECREF(pResult);
|
|
|
-
|
|
|
- Py_Finalize();
|
|
|
-
|
|
|
- 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(
|
|
|
- NULL,
|
|
|
- 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;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-BOOL ReadFromPipe(HANDLE hStdOutRead)
|
|
|
-{
|
|
|
- char out_buffer[BUFSIZE] = {0};
|
|
|
- DWORD dwRead;
|
|
|
- BOOL bSuccess = FALSE;
|
|
|
-
|
|
|
-
|
|
|
- bSuccess = ReadFile( hStdOutRead, out_buffer, BUFSIZE, &dwRead, NULL);
|
|
|
- if ((bSuccess) && (dwRead!=0))
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- OutputDebugString(out_buffer);
|
|
|
- }
|
|
|
- return bSuccess;
|
|
|
-}
|
|
|
-
|
|
|
-bool GetOutput( HANDLE hStdOutRead, int timeout )
|
|
|
-{
|
|
|
- if( NULL == hStdOutRead )
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- char buffer[4096] = {0};
|
|
|
- DWORD readBytes = 0;
|
|
|
- while( timeout > 0 )
|
|
|
- {
|
|
|
-
|
|
|
- if( FALSE == PeekNamedPipe( hStdOutRead, buffer, sizeof(buffer) - 1, &readBytes, 0, NULL ) )
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if( 0 == readBytes )
|
|
|
- {
|
|
|
- ::Sleep(200);
|
|
|
- timeout -= 200;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- readBytes = 0;
|
|
|
- if( ::ReadFile( hStdOutRead, buffer, sizeof(buffer) - 1, &readBytes, NULL) )
|
|
|
- {
|
|
|
- OutputDebugString(buffer);
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
+ return nRetCode;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-RUNPYTHON_API int CallPythonEx(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);
|
|
|
- GetStartupInfo(&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);
|
|
|
-
|
|
|
- SECURITY_ATTRIBUTES sa;
|
|
|
- sa.bInheritHandle = TRUE;
|
|
|
- sa.lpSecurityDescriptor = NULL;
|
|
|
- sa.nLength = sizeof(sa);
|
|
|
-
|
|
|
-
|
|
|
- HANDLE hStdOutRead, hStdOutWrite;
|
|
|
- if ( !CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0) )
|
|
|
- {
|
|
|
- printf("创建stdout管道失败\n");
|
|
|
- return -3;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- HANDLE hStdErrWrite;
|
|
|
- if ( !DuplicateHandle(GetCurrentProcess(), hStdOutWrite, GetCurrentProcess(), &hStdErrWrite, 0, TRUE, DUPLICATE_SAME_ACCESS) )
|
|
|
- {
|
|
|
- printf("创建stderr管道失败\n");
|
|
|
- return -3;
|
|
|
- }
|
|
|
-
|
|
|
- si.dwFlags |= STARTF_USESTDHANDLES;
|
|
|
-
|
|
|
- si.hStdOutput = hStdOutWrite;
|
|
|
-
|
|
|
- si.hStdError = hStdErrWrite;
|
|
|
-
|
|
|
-
|
|
|
- if( !CreateProcess(
|
|
|
- NULL,
|
|
|
- szCommandLine,
|
|
|
- NULL,
|
|
|
- NULL,
|
|
|
- TRUE,
|
|
|
- 0,
|
|
|
- NULL,
|
|
|
- NULL,
|
|
|
- &si,
|
|
|
- &pi )
|
|
|
- )
|
|
|
- {
|
|
|
- printf( "CreateProcess failed (%d).\n", GetLastError() );
|
|
|
- return -3;
|
|
|
- }
|
|
|
-
|
|
|
-#ifdef _DEBUG
|
|
|
- DWORD process_exit_code = 0;
|
|
|
- while (GetExitCodeProcess(pi.hProcess, &process_exit_code))
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- ReadFromPipe(hStdOutRead);
|
|
|
- if ( process_exit_code != STILL_ACTIVE )
|
|
|
- break;
|
|
|
- }
|
|
|
-#endif
|
|
|
-
|
|
|
-
|
|
|
- WaitForSingleObject( pi.hProcess, INFINITE );
|
|
|
-
|
|
|
-
|
|
|
- CloseHandle( pi.hProcess );
|
|
|
- CloseHandle( pi.hThread );
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|