in reply to Re^2: Win32::Process handle inheritance (don't use 'local' on file handles)
in thread Win32::Process handle inheritance

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re^3: Win32::Process handle inheritance (don't use 'local' on file handles)

Replies are listed 'Best First'.
Re^4: Win32::Process handle inheritance (works for me)
by tye (Sage) on May 13, 2008 at 14:29 UTC
    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