freebsdboy has asked for the wisdom of the Perl Monks concerning the following question:
use Win32::OLE qw(EVENTS); use Win32::Console; my $console = Win32::Console->new(STD_INPUT_HANDLE); my $refWMI = Win32::OLE->GetObject('winMgmts:'); my $refSink = Win32::OLE->new ('WBemScripting.SWbemSink'); Win32::OLE->WithEvents($refSink,\&eventCallback); my $strQuery = "SELECT * FROM __SessionEndingEventHandler " . "WITHIN 10 WHERE TargetInstance ISA '\Win32_Service\'"; $refWMI->ExecNotificationQueryAsync($refSink, $StrQuery); print "Waiting for Events ...\n"; my $continueLooping = 1; while ($continueLooping) { if ($console->GetEvents()!= 0){ my @cons = $console->Input(); if ($cons[1] != 0){ undef $countingLooping; } } Win32::OLE->SpinMessageLoop(); Win32::Sleep(500); } $refSink->Cancel(); Win32::OLE->WithEvents($refSink); undef $refSink; undef $refWMI; undef $console; print "Finished.\n"; sub eventCallback() { my ($refSource,$refEventName,$refEvent,$refEvent,$refContext) += @_; if ($refEventName eq "onObjectReady") { print $refEvent->TargetInstance->{Name} . " has " . ($refEvent->TargetInstance->{Started} ? "started" : "stopped") . "\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Catching system events, via WMI
by igelkott (Priest) on Feb 09, 2008 at 22:38 UTC | |
by freebsdboy (Novice) on Feb 12, 2008 at 03:48 UTC | |
by igelkott (Priest) on Feb 12, 2008 at 20:47 UTC |