> >*** WMI Event sample - > >* When a new printjob is started, echo the originating pc and page count >* RELEASE oEventCatcher in the command window to stop. > >public oEventCatcher > >oEventCatcher = createobject("eventcatcher") > >cQuery = [select * from __InstanceCreationEvent within 0.1 where TargetInstance ISA 'Win32_PrintJob'] >oEventCatcher.CatchEvents(cQuery) > >return > >** Event catching class > >define class eventcatcher as relation > > oSink = .null. > oWbemSink = .null. > oWMI = .null. > > procedure init > > this.oWMI = getobject("winmgmts:") > * Create the sink objects and bind them > this.oWbemSink = createobject("wbemscripting.swbemsink") > this.oSink = createobject("vfpsink") > eventhandler(this.oWbemSink, this.oSink) > > endproc > > procedure destroy > > if vartype(this.oWbemSink) = "O" > this.oWbemSink.Cancel() > endif > > endproc > > procedure CatchEvents > lparameters cQuery > * Send the WMI sink not the VFP one. > this.oWMI.ExecNotificationQueryAsync(this.oWbemSink, cQuery) > endproc > >enddefine > >** The sink class is easier to manage when decoupled from the eventcatcher class > >define class vfpsink as relation > > implements ISWbemSinkEvents in "WbemScripting.SWbemSink" > > procedure ISWbemSinkEvents_OnObjectReady(oObject, oAsyncContext) > ? "Originating PC: " + oObject.TargetInstance.HostPrintQueue > ? "Pages: " + transform(oObject.TargetInstance.TotalPages) > endproc > > procedure ISWbemSinkEvents_OnCompleted(nResult, oErrorObject, oAsyncContext) > procedure ISWbemSinkEvents_OnProgress(nUpperBound, nCurrent, cMessage, oAsyncContext) > procedure ISWbemSinkEvents_OnObjectPut(oObjectPath, oAsyncContext) > >enddefine >