[英]Capture stdout for a remote process' child process
Currently working on a situation where I need to capture the stdout/stderr for a child process of an external process.目前正在处理一种情况,我需要为外部进程的子进程捕获 stdout/stderr。 For this situation would I need a named pipe?
对于这种情况,我需要一个命名管道吗?
I have currently tried the following:我目前尝试了以下方法:
SECURITY_ATTRIBUTES saAttr;
HANDLE hStdOutRd = NULL;
HANDLE hStdOutWr = NULL;
HANDLE hDupStdOutWr = NULL;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
CreatePipe(&hStdOutRd, &hStdOutWr, &saAttr, 0) )
SetHandleInformation(hStdOutRd, HANDLE_FLAG_INHERIT, 0) )
HANDLE hExProc = OpenProcess(PROCESS_DUP_HANDLE, false, EXTERNAL_PROC);
DuplicateHandle(GetCurrentProcess(),
hStdOutWr,
hExProc,
&hDupStdOutWr,
0,
TRUE,
DUPLICATE_SAME_ACCESS);
// Child process for EXTERNAL_PROC executes and should inherit the duplicated handle
// Read from hStdOutRd
When reading from the pipe, it either hangs or doesn't return anything.从管道读取时,它要么挂起,要么不返回任何内容。 I'm assuming this is the case that it is an anonymous pipe and only works for parent-child processes and not external processes.
我假设这是一个匿名管道,仅适用于父子进程而不适用于外部进程。 I would prefer to not use a Named Pipe, would it be possible to DuplicateHandle() the pipes back from the remote process to my process and then access the data?
我不想使用命名管道,是否可以将管道从远程进程返回到我的进程,然后访问数据? If so, what would that look like?
如果是这样,那会是什么样子?
No, you do not need to use a named pipe, an anonymous pipe will do fine.不,您不需要使用命名管道,匿名管道就可以了。 MSDN provides a full example of capturing output from a child process:
MSDN 提供了一个从子进程捕获输出的完整示例:
Creating a Child Process with Redirected Input and Output 创建具有重定向输入和输出的子进程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.