Преглед изворни кода

使用以下方法,能输出本进程的printf内容,但是脚本的print输出不了,与上一变更内容刚好互补就能实现所需需求;
// 创建stdout的管道;
if (!CreatePipe(&m_hStdOutRead, &m_hStdOutWrite, NULL, 0))
{
printf("Error:创建stdout管道失败\n");
return -1;
}

int wpfd = _open_osfhandle((intptr_t)m_hStdOutWrite, _O_TEXT);
FILE* pOutFile = _fdopen(wpfd, "w");
*stdout = *pOutFile;
setvbuf(stdout, NULL, _IONBF, 0);

scbc.sat2 пре 5 година
родитељ
комит
5cbdbd8770
1 измењених фајлова са 10 додато и 11 уклоњено
  1. 10 11
      RunPython/RunPython/ScriptExecutor.cpp

+ 10 - 11
RunPython/RunPython/ScriptExecutor.cpp

@@ -2,6 +2,7 @@
 #include "ScriptExecutor.h"
 #include <fstream>
 #include <io.h>
+#include <fcntl.h>
 
 CScriptExecutor::CScriptExecutor(void)
 {
@@ -160,6 +161,11 @@ int CScriptExecutor::RedirectStdout(LPSTARTUPINFO si /*=NULL*/)
 		if((stream = freopen(m_szLogPath, "w", stdout)) == NULL)
 			return -1;
 #else
+		SECURITY_ATTRIBUTES sa;
+		sa.bInheritHandle = TRUE;
+		sa.lpSecurityDescriptor = NULL;
+		sa.nLength = sizeof(sa);
+
 		// 创建stdout的管道;
 		if (!CreatePipe(&m_hStdOutRead, &m_hStdOutWrite, NULL, 0))
 		{
@@ -167,17 +173,10 @@ int CScriptExecutor::RedirectStdout(LPSTARTUPINFO si /*=NULL*/)
 			return -1;
 		}
 
-		int wpfd = _open_osfhandle((intptr_t)m_hStdOutWrite, 0);
-		if (_dup2(wpfd, _fileno(stdout)) != 0) 
-		{
-			printf("Error:dup2调用失败");
-		}
-		_close(wpfd);
-
-		if (!SetStdHandle(STD_OUTPUT_HANDLE, m_hStdOutWrite))
-		{
-			printf("Error:重定向失败\n");
-		}
+		int wpfd = _open_osfhandle((intptr_t)m_hStdOutWrite, _O_TEXT);
+		FILE* pOutFile = _fdopen(wpfd, "w");
+		*stdout = *pOutFile;
+		setvbuf(stdout, NULL, _IONBF, 0);
 #endif
 	}