Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

get ManagementObject from EventArrivedEventArgs

$
0
0
Platform: Windows XP Profressional with SP3, .NET 3.5
Language: VC++
References: System.Management

I'm using the ManagementEventWatcher to monitor new instances of Win32_PrintJob.  I want to be able to quickly use the ManagementObject->InvokeMethod() method to pause the new print job.  I want to confirm that there is no way to get the ManagementObject from the EventArrivedEventArgs, and that I have to make a new ManagementObjectSearcher object and query for a ManagementObjectCollection as follows:

// Pull out JobId from the ManagementBaseObject inside of the event arg
System::String^ jobid = (
(System::Management::ManagementBaseObject^)in_event_args->NewEvent->Properties["TargetInstance"]->Value
)->Properties["JobId"]->Value->ToString();

// Query for ManagementObjects with the jobid (should only be one)
System::Management::ManagementObjectCollection::ManagementObjectEnumerator^ moe = (
gcnew System::Management::ManagementObjectSearcher(
"\\\\" + System::Environment::MachineName + "\\root\\CIMV2",
"SELECT * FROM Win32_PrintJob WHERE JobId='" + jobid + "'"
)
)->Get()->GetEnumerator();
moe->MoveNext();

// Get the object and pause the job
System::Management::ManagementObject^ mo = (System::Management::ManagementObject^)moe->Current;
mo->InvokeMethod( "Pause", nullptr );
I'd really like to avoid having to re-query inside of the callback, but as far as I can find, I have to. 
Is there a way to get the job's ManagementObject from the EventArrivedEventArgs?  Alternatively, is there a better way to watch for and pause print jobs all together?

Thanks!

Viewing all articles
Browse latest Browse all 8156


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>