Jump to content

Creating child process with redirected I/O


Recommended Posts

Hi All,

I am a new member here, so first up Greetings to all members and hats off to the creators of this amazing language (I am sure you guys must have heard this an awful lot of times).

I was trying to create a child process with redirected I/O using the Windows APIs. I have used anonymous pipes for the I/O redirection. I created two pipes, one for child's STDIN (hereafter referred to as input pipe) and the other for child's STDOUT (hereafter referred to as output pipe). I am not using STDERR. I have used _WinApi_SetHandleInformation to ensure that the child does not inherit write handle of input pipe and the read handle of output pipe.

Once the child process gets created, I can get the child to write something to its STDOUT and read off of it from the parent side. This works like a charm.

However, if I write something to the input pipe from parent and try reading it from the child using _WinApi_ReadFile, the function fails immediately with INVALID_HANDLE_VALUE.

GetLastErrorMessage shows the corresponding error message - handle is invalid. I have verified that WriteFile from parent succeeds in writing the data.

I have also noticed that GetLastErrorMessage returns "Specified procedure could not be found" after _WinApi_CreateProcess, which doesn't make sense to me since the child process does get created.

I have tried everything I could think of. Google hasn't been able to help much.

Please see attached for the parent and child side codes.

Any help here will be greatly appreciated.

Thanks in advance!

Redirected IO.zip

Link to comment
Share on other sites

Well yeah, I have those options, but I really want to do this via Windows APIs, for one this is a learning process for me.

Secondly, the code is only a part of the entire thing (which will follow once this gets sorted out) and I'll need to take care of inheritance of handles.

Third, I would have implemented the solution that you suggested (maybe I will have to since this is not getting resolved), I want to find out what is wrong with the code or why is it not working just for the sake of satisfying my curiosity. Just getting something to work or abandon something not working is really not what I do. I need a reason for it not working or working. I hope you understand what I am trying to say.

Thanks!

Link to comment
Share on other sites

Hi...There has been a small goof up with the code. Please locate this line: _WinAPI_SetHandleInformation ($hndInputPipe_Rd, $HANDLE_FLAG_INHERIT, 0).

This should be corrected to: _WinAPI_SetHandleInformation ($hndInputPipe_Wr, $HANDLE_FLAG_INHERIT, 0) as its the write handle of input pipe I don't want the child to inherit.

Rest is the same.

Thanks !!

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...