#  DaemonDebugPipe.pl
#  Example 8.12:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script is used to debug a Win32 service script. The service
#  opens a connection to this named pipe and prints any output it
#  needs.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32::Pipe;

$PIPE_NAME = "Debug Logging";
if( $Pipe = new Win32::Pipe( $PIPE_NAME, DEFAULT_WAIT_TIME, PIPE_READMODE_BYTE ) )
{
    Announce();
    while( $Pipe->Connect() )
    {
        my $User = ($Pipe->GetInfo())[2];
        print "Pipe opened by $User at " . localtime(). "\n\n";
        while( my $In = $Pipe->Read() )
        {
            print $In;
        }
        print "\nDisconnecting...\n";
        $Pipe->Disconnect();
        Announce();
    }
    $Pipe->Close();
}
else
{
    print "\nCould not create pipe\n";
    print "Error: " . Win32::FormatMessage( $Win32::Pipe::Error ) . "\n";
}

sub Announce
{
    my $PipeName = "\\\\" . Win32::NodeName() . "\\pipe\\$PIPE_NAME";
    print "-----------------------------------\n";
    print "R e a d y   f o r   r e a d i n g :\n";
    print "$PipeName is waiting for a client to connect...\n";
}