0

Currently working on a situation where I need to capture the stdout/stderr for a child process of an external process. 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?

10
  • This is operating system specific. Without mention of an OS, your question is unclear. Perhaps you should consider frameworks like POCO or Qt Mar 6, 2018 at 7:20
  • @BasileStarynkevitch the code in question is using Win32 API functions so clearly it is meant to run on Windows only. Mar 6, 2018 at 8:20
  • 2
    named pipes the same as un-nmamed or so called anonymous pipe. at you need use asynchronous io for never hung. and CreatePipe is bad for this target. much more better use CreateNamedPipe +CreateFile. DuplicateHandle - and how you notify child of duplicated value ?
    – RbMm
    Mar 6, 2018 at 9:24
  • 1
    and before win7 - CreatePipe simply create 2 random named named pipes. begin from win7 - possible create exactly unnamed pair. say CreatePipe always create synchronous pipes. but we can create it yourself, direct as asynchronous. can just create one inheritable and one - no. can create both duplex. really CreatePipe very restricted api.
    – RbMm
    Mar 6, 2018 at 12:03
  • 1
    and this is very relevant - need clear understand - not exist 2 different pipe types(named vs un-named or random named) - exist simply pipes and all. simply exist single documented api call CreatePipe which create unnamed pipe pair - with simply interface but restricted customization (set pipe properties). all. operations is the same. and possible create un-named pair direct (via native calls only) with any wanted properties
    – RbMm
    Mar 6, 2018 at 12:08

1 Answer 1

0

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:

Creating a Child Process with Redirected Input and Output

7
  • named pipe and an anonymous pipe - is absolute the same object. and msdn example from my opinion pretty bad.
    – RbMm
    Mar 6, 2018 at 9:26
  • 1
    OP is asking about capturing output from a process that he didn't launch (handle obtained via OpenProcess()), which I believe is not (easily) doable.
    – zett42
    Mar 6, 2018 at 9:44
  • @zett42 I didn't notice the OpenProcess() part. In that case, you can't redirect output for a process that you did not launch yourself. Mar 6, 2018 at 17:14
  • what if I launch the process myself with CreateProcess() and STARTUPINFOEX + PROC_THREAD_ATTRIBUTE_PARENT_PROCESS? Would duplicating the handle to the remote parent I specify and setting bInheritHandle get me access to the child's stdout? Mar 6, 2018 at 18:31
  • @NotWarrenBuffet: regardless of which process you assign as the child's parent, when you call CreateProcess() directly, you can provide your own STD(IN|OUT|ERR) pipes for the new child via the STARTUPINFO/EX struct, just like the article I linked to in my answer demonstrates. You don't need to duplicate anything. Enable the STARTF_USESTDHANDLES flag in the dwFlags field, and then assign the read end of a pipe to the hStdOut and hStdError fields. Mar 6, 2018 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.