简体   繁体   English

捕获远程进程的子进程的标准输出

[英]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.

相关问题 捕获生成的进程标准输出作为unicode - Capture spawned process stdout as unicode 将子进程的stdout重定向到父进程stdin - redirect the stdout of a child process to the parent process stdin 以编程方式从父级的子进程中捕获打印,因此它们不会进入stdout - Programatically capture prints from child process at parent so they don't go to stdout 连接到子进程的stdout的管道中的消息不完整 - Incomplete messages in pipe connected to stdout of child process 如何实时获取子进程的标准输出? - how to get stdout of child process in realtime? 是否可以将子进程的标准输出重定向到父进程中的另一个文件? - Is it possible to redirect child process's stdout to another file in parent process? 使用BOOST进程在单独的线程中读取子进程标准输出 - Read child process stdout in a separate thread with BOOST process 如何将子进程的标准输出(stderr)重定向到winapi中的套接字? - How redirect stdout (stderr) of child process to socket in winapi? 如何在不阻塞或不轮询的情况下从子进程的STDOUT接收输出 - How To Receive Output From A Child Process' STDOUT Without Blocking or Poling 在某些情况下,重定向子进程的STDOUT时应用程序冻结 - Application freeze when redirecting STDOUT of child process in some cases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM