Hi bulk88,
Thanks for helping me out, below is the final code I made that seems to be working fine.
#!/usr/bin/perl -w
use strict;
use Gtk2 '-init';
use Gtk2::Helper;
use Data::Dumper;
use FileHandle;
use IPC::Open2;
use Win32API::File;
use Win32;
use Win32::API;
{
my $api;
die "PeekNamedPipe"
if ! ($api= Win32::API->Import("kernel32", "
BOOL PeekNamedPipe(
HANDLE hNamedPipe,
LPVOID lpBuffer,
DWORD nBufferSize,
LPDWORD lpBytesRead,
LPDWORD lpTotalBytesAvail,
LPDWORD lpBytesLeftThisMessage
);"));
}
my $wfh = FileHandle->new();
my $rfh = FileHandle->new();
open2($rfh,$wfh,"C:\\Windows\\System32\\cmd.exe");
my $hnd = Win32API::File::FdGetOsFHandle($rfh->fileno()); #get Win32 k
+ernel handle from Perl land
if($hnd == Win32API::File::INVALID_HANDLE_VALUE()) {
die "bad hnd";
}
my $tag = Gtk2::Helper->add_watch($rfh, 'in',\&preview_call);
my $window = Gtk2::Window->new();
$window->signal_connect("destroy",sub{Gtk2->main_quit();});
$window->set_size_request(300,300);
my $hbox = Gtk2::HBox->new();
$window->add($hbox);
my $button = Gtk2::Button->new("A");
$button->signal_connect('clicked'=>sub{
add_job();
});
$hbox->pack_start($button,1,1,0);
$button = Gtk2::Button->new("B");
$button->signal_connect('clicked'=>sub{
print "i do nothing\n";
});
$hbox->pack_start($button,1,1,0);
$window->show_all();
Gtk2->main();
sub preview_call {
my $bRead = 0;
my $bLeft = 0;
my $bAvail = 0;
my $ret = 0;
my $buffer;
$bAvail = "\x00" x 4;
$ret = PeekNamedPipe($hnd,undef,0,undef,$bAvail,undef);
if(!$ret) {
my $err = Win32::GetLastError();
die "PNP failed $err $^E";
}
$bAvail = unpack('L', $bAvail) . "\n";
if($bAvail > 0) {
print "Available: $bAvail\n";
sysread($rfh,$buffer,$bAvail);
print $buffer . "\n";
}
while (Gtk2->events_pending()) {Gtk2->main_iteration();}
return 1;
}
sub add_job {
print $wfh "dir\n\n";
}
thanks a bunch again! I hope somebody finds this helpful too!
Mabuhay Civil Engineers! :D