I recently looked again at PPerl, because I wanted to fix some other behaviour, and found that it doesn't build on Windows. After a quick converstation with the author, Matt Sergeant, I found that PPerl doesn't cater for Windows because the functionality it needs, sendfd() and SOCK_PERM, only exists on Linux and some other unixish OSes and not on Windows.

Almost by chance, I happened over the DuplicateHandle() API of Windows, which allows to duplicate a file handle for use in another process. My test file implementing this is below.

I use a child process, to which I print the handle number on STDIN. The child process then reads from that filehandle which it knows no name of. With a child process, the same could well have been done using input/output redirection, but the use of a child process is only to alleviate permission issues and general coordination issues, like how the two processes find each other.

#!perl -w use strict; use Win32::API; use Win32; use Win32API::File qw(FdGetOsFHandle); Win32::API->Import( 'kernel32', 'DuplicateHandle', 'NNNPNNN', 'N', ); Win32::API->Import( 'kernel32', 'OpenProcess', 'NNN', 'N', ); # This is the child process that reads the number from STDIN, opens th +at handle, and reads from it my $cmd = q(| perl -MWin32API::File=OsFHandleOpen -e "$fileno=0+<> or +die;OsFHandleOpen(FH, $fileno,'r') or die $!; print for <FH>"); my $child = open my $child_fh, $cmd or die "Can't spawn child [$cmd]: $! / $? / $^E"; print "1..2\n"; print "1 "; print $child ? "" : "not "; print "ok # spawned child\n"; # 0x400 for PROCESS_QUERY_INFORMATION # 0x040 for PROCESS_DUP_HANDLE my $childHandle = OpenProcess(0x440,0,$child) or die $^E; my $buf = "\0\0\0\0"; my $fhandle = FdGetOsFHandle( fileno *DATA ); DuplicateHandle(-1, $fhandle, $childHandle, $buf, 0, 1, 3 ) or die $^E; $buf = unpack "V", $buf; # Now tell our child from what handle it should read print {$child_fh} "$buf\n"; # The parent is done close $child_fh; __DATA__ 2 ok # output read from child process

As is the case on Windows, sockets are outside of this scheme, unfortunately. If you want to pass around sockets between processes, you need to look at WSADuplicateSocket


In reply to Pass filehandles around between (unrelated) processes on Windows by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.