use strict;
use Win32;
use Win32::API;
use Win32API::File qw( :Func );
use constant {
PIPE_NAME => "//./pipe/X"
, PIPE_ACCESS_INBOUND => 0x00000001
, FILE_FLAG_FIRST_PIPE_INSTANCE => 0x00080000
, PIPE_TYPE_MESSAGE => 0x00000004
, PIPE_READMODE_MESSAGE => 0x00000002
};
my $NULL = 0;
# Note warning on LPSECURITY_ATTRIBUTES, but used only as NULL pointer
Win32::API->Import('kernel32'
,'HANDLE CreateNamedPipe( LPCTSTR lpName '
. ', DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances'
. ', DWORD nOutBufferSize, DWORD nInBufferSize'
. ', DWORD nDefaultTimeOut'
. ', LPSECURITY_ATTRIBUTES lpSecurityAttributes)' );
# Note warning on LPOVERLAPPED, but used only as NULL pointer.
Win32::API->Import('kernel32'
,'BOOL ConnectNamedPipe(HANDLE hNamedPipe'
. ', LPOVERLAPPED lpOverlapped )');
Win32::API->Import('kernel32'
,'BOOL DisconnectNamedPipe(HANDLE hNamedPipe)');
my $pipehandle = CreateNamedPipe( PIPE_NAME
, PIPE_ACCESS_INBOUND + FILE_FLAG_FIRST_PIPE_INSTANCE
, PIPE_TYPE_MESSAGE + PIPE_READMODE_MESSAGE
, 1 , 512 , 512 , 10000 , $NULL )
or die "Unable to create pipe:". Win32::GetLastError() ;
my( $res, $message);
while( 1 ) {
ConnectNamedPipe($pipehandle,$NULL)
or die "Connect failed: ". Win32::GetLastError();
$res = ReadFile($pipehandle,$message, 512, [], [] );
print "$message\n";
$res = DisconnectNamedPipe($pipehandle);
if( $message eq "exit" ) {
last;
}
}
$res = CloseHandle($pipehandle);
Client side:
use strict;
use Win32;
use Win32API::File qw( :Func :Misc :GENERIC_ );
use constant {
PIPE_NAME => "//./pipe/X"
};
my $pipehandle;
while( ! $pipehandle ) {
# Open client side of pipe.
$pipehandle = CreateFile( PIPE_NAME, GENERIC_WRITE, 0, []
, OPEN_EXISTING , 0, [] );
if( ! $pipehandle ) { # Error during opening
my $e = Win32::GetLastError();
my $m = Win32::FormatMessage($e);
print "Error ($e):$m\n";
exit;
}
}
#write the message and close
my $res = WriteFile($pipehandle, $ARGV[0], length($ARGV[0]), [], [] );
my $res = CloseHandle($pipehandle);
20081129 Janitored by Corion: Changed PRE to P tags, as per Writeup Formatting Tips |