My goal is to capture the output of netstat and getmac without flashing up a command window every time I call them (this is on Windows 7).

The stripped down code I have so far (place in sandbox.pl):

use strict; use warnings; use Win32::Process; use Win32; use Data::Dumper; my $netstat_obj; my $create_status = Win32::Process::Create( $netstat_obj, 'C:/Windows/System32/netstat.exe', 'netstat -a -e -n -o -s -r' +, 0, DETACHED_PROCESS | CREATE_NO_WINDOW | CREATE_SUSPENDED, 'C:/temp/' ) || die Win32::FormatMessage(Win32::GetLastError() +); print Data::Dumper->new(['1', $create_status, $netstat_obj], ['where__ +________', 'create_status', 'netstat_obj'])->Indent(1)->Sortkeys(1)-> +Useqq(1)->Deparse(1)->Dump(); my $NETSTAT_FH; my $open_status = Win32::Process::Open( $NETSTAT_FH, $netstat_obj->GetProcessID(), 0 ) || die Win32::FormatMessage(Win32::GetLastError()); print Data::Dumper->new(['2', $open_status, $NETSTAT_FH], ['where_____ +_____', 'open_status', 'NETSTAT_FH'])->Indent(1)->Sortkeys(1)->Useqq( +1)->Deparse(1)->Dump(); $netstat_obj->Resume(); my $netstat_data = ''; { my $offset = 0; my $length = 1024; my $buffer = undef; while(my $bytes_read = $NETSTAT_FH->sysread($buffer, $length, $offse +t)) { if(defined $bytes_read) { $netstat_data .= $buffer; $buffer = undef; last if($bytes_read == 0); $offset += $bytes_read; } else { die "ERROR: Could not read from NETSTAT_FH: $! "; } } } $netstat_obj->Wait(2000); my $netstat_exit; $netstat_obj->GetExitCode($netstat_exit); print Data::Dumper->new(['3', $netstat_obj, $NETSTAT_FH, $netstat_data +], ['where__________', 'netstat_obj', 'NETSTAT_FH', 'netstat_data'])- +>Indent(1)->Sortkeys(1)->Useqq(1)->Deparse(1)->Dump();


The results of running the above code:

C:\temp>perl.exe sandbox.pl $where__________ = 1; $create_status = 1; $netstat_obj = bless( do{\(my $o = 1776208)}, 'Win32::Process' ); $where__________ = 2; $open_status = 1; $NETSTAT_FH = bless( do{\(my $o = 1777552)}, 'Win32::Process' ); Your vendor has not defined Win32::Process macro sysread, used at sand +box.pl line 32. (Error was: 'Invalid argument') at C:/xampp/perl/site/lib/Win32/Process.pm line 53.


What I am trying to do in the above code is:

  1. create a new (detached and suspended) Windows process sans command window
  2. open a filehandle to the newly created process for reading said process's STDOUT
  3. read all of the data from the filehandle
  4. and wait for netstat to exit, then find its exit code
As the above output demonstrates, I am stuck on #3 (and despite the CREATE_NO_WINDOW constant, a window is still being created :-/ ).

In place of the sysread, I have tried: read, getlines, and getline; all with the same error "Your vendor has not defined Win32::Process macro X, used at …", where X is one of the aforementioned methods.

So my question has a few parts:

Thank-you for your thoughts and suggestions,
IOl01


In reply to How do I communicate with a Windows process? by IOl01

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.