|
@@ -44,7 +44,7 @@ DWORD __stdcall CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
|
|
|
if ( that->m_nRunType == EMBEDDED )
|
|
|
{
|
|
|
- that->RedirectStdout();
|
|
|
+ that->RedirectProcessStdout();
|
|
|
ResumeThread(that->m_hLogThread);
|
|
|
that->RunEmbeddedScript();
|
|
|
}
|
|
@@ -109,12 +109,12 @@ DWORD __stdcall CScriptExecutor::_LogExportThread(LPVOID lpParam)
|
|
|
that->m_hLogThread = NULL;
|
|
|
|
|
|
// 结束重定向;
|
|
|
- that->EndRedirectStdOut();
|
|
|
+ that->EndSubprocessStdOut();
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int CScriptExecutor::RedirectStdout(LPSTARTUPINFO si /*=NULL*/)
|
|
|
+int CScriptExecutor::RedirectSubprocessStdout(LPSTARTUPINFO si /*=NULL*/)
|
|
|
{
|
|
|
if (m_nRunType == SUBPROCESS)
|
|
|
{
|
|
@@ -145,7 +145,16 @@ int CScriptExecutor::RedirectStdout(LPSTARTUPINFO si /*=NULL*/)
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
- else if (m_nRunType == EMBEDDED)
|
|
|
+
|
|
|
+ // 异常类型;
|
|
|
+ printf("Error:异常类型\n");
|
|
|
+
|
|
|
+ return -5;
|
|
|
+}
|
|
|
+
|
|
|
+int CScriptExecutor::RedirectProcessStdout()
|
|
|
+{
|
|
|
+ if (m_nRunType == EMBEDDED)
|
|
|
{
|
|
|
// 创建stdout的管道;
|
|
|
if (!CreatePipe(&m_hStdOutRead, &m_hStdOutWrite, NULL, 0))
|
|
@@ -287,7 +296,9 @@ int CScriptExecutor::RunEmbeddedScript()
|
|
|
}
|
|
|
|
|
|
// 初始化Python环境;
|
|
|
- Py_Initialize();
|
|
|
+ if (!Py_IsInitialized())
|
|
|
+ Py_Initialize();
|
|
|
+
|
|
|
if (!Py_IsInitialized())
|
|
|
{
|
|
|
printf("Error:初始化Python环境失败\n");
|
|
@@ -380,7 +391,7 @@ int CScriptExecutor::RunScripProcess()
|
|
|
_stprintf_s(szCommandLine, _T("python -W ignore %s"), m_szScriptPath);
|
|
|
|
|
|
// 重定向输出;
|
|
|
- RedirectStdout(&m_si);
|
|
|
+ RedirectSubprocessStdout(&m_si);
|
|
|
// 恢复日志线程;
|
|
|
ResumeThread(m_hLogThread);
|
|
|
|
|
@@ -529,9 +540,9 @@ BOOL CScriptExecutor::EndSubprocess()
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-void CScriptExecutor::EndRedirectStdOut()
|
|
|
+void CScriptExecutor::EndProcessStdOut()
|
|
|
{
|
|
|
- OutputDebugString("Error:_dup2分配文件描述符失败\n");
|
|
|
+ OutputDebugString("--------------EndProcessStdOut----------------\n");
|
|
|
#if 0 // 恢复失败,代码有问题
|
|
|
// 恢复标准输出(嵌入进程脚本);
|
|
|
if ( m_nRunType == EMBEDDED && m_hOldStdOutWrite)
|
|
@@ -554,7 +565,6 @@ void CScriptExecutor::EndRedirectStdOut()
|
|
|
}
|
|
|
}
|
|
|
#endif
|
|
|
- printf("Error:_dup2分配文件描述符失败\n");
|
|
|
|
|
|
// 关闭重定向句柄;
|
|
|
if (m_hStdErrorWrite)
|
|
@@ -562,11 +572,27 @@ void CScriptExecutor::EndRedirectStdOut()
|
|
|
m_hStdErrorWrite = NULL;
|
|
|
|
|
|
// 只有子进程方式才关闭句柄;
|
|
|
- if ( m_nRunType == SUBPROCESS && m_hStdOutWrite)
|
|
|
- {
|
|
|
+ if ( m_hStdOutWrite)
|
|
|
CloseHandle(m_hStdOutWrite);
|
|
|
- m_hStdOutWrite = NULL;
|
|
|
- }
|
|
|
+ m_hStdOutWrite = NULL;
|
|
|
+
|
|
|
+ if (m_hStdOutRead)
|
|
|
+ CloseHandle(m_hStdOutRead);
|
|
|
+ m_hStdOutRead = NULL;
|
|
|
+}
|
|
|
+
|
|
|
+void CScriptExecutor::EndSubprocessStdOut()
|
|
|
+{
|
|
|
+ OutputDebugString("--------------EndSubprocessStdOut----------------\n");
|
|
|
+ // 关闭重定向句柄;
|
|
|
+ if (m_hStdErrorWrite)
|
|
|
+ CloseHandle(m_hStdErrorWrite);
|
|
|
+ m_hStdErrorWrite = NULL;
|
|
|
+
|
|
|
+ // 只有子进程方式才关闭句柄;
|
|
|
+ if ( m_hStdOutWrite)
|
|
|
+ CloseHandle(m_hStdOutWrite);
|
|
|
+ m_hStdOutWrite = NULL;
|
|
|
|
|
|
if (m_hStdOutRead)
|
|
|
CloseHandle(m_hStdOutRead);
|