in reply to Win32::Process handle inheritance

open(SAVEOUT, ">&STDOUT") before redirecting STDOUT and creating the process, and open(STDOUT, ">&SAVEOUT") and close SAVEOUT after.

Replies are listed 'Best First'.
Re^2: Win32::Process handle inheritance (don't use 'local' on file handles)
by tye (Sage) on May 13, 2008 at 12:42 UTC

    You also have to get rid of the local *STDOUT; (which hides away the current STDOUT which prevents file handle 1 from being reused which is all that C code and child processes care about).

    - tye        

      The real problem here is that the InheritHandles flag on CreateProcess() relates to OS handles (of all types), not Perl filehandles or C-runtime file numbers or FILE* structs. Nothing you do to the current process Perl handles or CRT handles will have any influence upon the created process at all.

      Setting the Inherit Handles flag will cause the process created to inherit access to exists OS handles of the current process. But in order for the new process to inherit redirections of it's STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE handles, it is necessary to pass the handles that it should inherit as a part of the STARTUPINFO struct on the CreateProcess() and set the STARTF_USESTDHANDLES flag. Only then would the OS got the information required to make the appropriate redirection(s). This functionality is not visible via Win32::Process.

      I did manage to get this to work from Perl using Win32::API a couple of years ago, but that was before I upgraded my 0.41 installation. Since then, trying pretty much anything with Win32::API breaks :(

      That's problem when people with no real interest in the OS start messing with things just to try and make it so that other people with no real interest in the OS can avoid using it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        The real problem here is [...] Nothing you do to the current process Perl handles or CRT handles will have any influence upon the created process at all.

        Actually, no, it just works:

        X:\> type stdout.pl #!/usr/bin/perl -w use strict; require Win32::Process; open STDOUT, "> stdout.txt" or die "Can't write stdout.txt: $!\n"; my $proc; Win32::Process::Create( $proc, $ENV{COMSPEC}, "/c dir", 1, 0, "." ) or die "Can't run 'dir': $^E\n"; X:\> perl stdout.pl X:\> type stdout.txt Volume in drive X is NSA_SECRETS Volume Serial Number is 007A-DEAD Directory of X:\ 2008-05-13 07:29 246 stdout.pl 2008-05-13 07:29 0 stdout.txt 2 File(s) 246 bytes X:\>

        (output trimmed to protect national secrets.)

        To save/restore the parent's original STDOUT, just follow the examples briefly described by ysth that are more fully covered in that standard open documentation.

        - tye        

Re^2: Win32::Process handle inheritance
by Anonymous Monk on May 13, 2008 at 11:16 UTC
    It is not working for Win32::Process::Create.