TheCNClrLib.Management
namespace contains interfaces that provide access to management information and
management events about the system, devices, and applications instrumented to
the Windows Management Instrumentation (WMI) infrastructure. These namespaces
also contain types necessary for instrumenting applications so that they expose
their management information and events through WMI to potential
customers.
These interfaces are defined
as classes and interfaces in C#
System.Management Namespaces.
The
following example shows how the client receives notification when an instance
ofWin32_Processis created because the event class
is__InstanceCreationEvent.
For more information, see the Windows Management Instrumentation documentation
in the MSDN Library at http://msdn.microsoft.com/library. The client receives
events synchronously by calling theWaitForNextEventmethod. This example can
be tested by starting a process, such as Notepad, while the example code is
running.
Delphi
// This example shows synchronous consumption of events.
// The client is blocked while waiting for events.
programEventWatcherPolling;{$APPTYPE CONSOLE}{$R *.res}usesSysUtils,CNClrLib.Management,CNClrLib.Core;varConsole:_Console;query:_WqlEventQuery;watcher:_ManagementEventWatcher;e:_ManagementBaseObject;begintryConsole:=CoConsole.CreateInstance;// Create event query to be notified within 1 second of
// a change in a service
query:=CoWqlEventQuery.CreateInstance('__InstanceCreationEvent',CoTimeSpan.CreateInstance(0,0,1),'TargetInstance isa ''Win32_Process''');// Initialize an event watcher and subscribe to events
// that match this query
watcher:=CoManagementEventWatcher.CreateInstance;watcher.Query:=query.AsEventQuery;// times out watcher.WaitForNextEvent in 5 seconds
watcher.Options.Timeout:=CoTimeSpan.CreateInstance(0,0,5);// Block until the next event occurs
// Note: this can be done in a loop if waiting for
// more than one occurrence
Console.WriteLine_14('Open an application (notepad.exe) to trigger an event.');e:=watcher.WaitForNextEvent;//Display information from the event
Console.WriteLine_17('Process {0} has been created, path is: {1}',CoManagementBaseObject.Wrap(e['TargetInstance'])['Name'],CoManagementBaseObject.Wrap(e['TargetInstance'])['ExecutablePath']);//Cancel the subscription
watcher.Stop;exceptonE:ExceptiondobeginConsole.WriteLine_14(E.message);end;end;end.
Example 2
The
following example demonstrates how theManagementPathclass parses a path to a WMI object. The path that
is parsed in the example is a path to an instance of a class.
Delphi
// This example shows synchronous consumption of events.
// The client is blocked while waiting for events.
programSample;{$APPTYPE CONSOLE}{$R *.res}usesSysUtils,CNClrLib.Management,CNClrLib.Core;varConsole:_Console;p:_ManagementPath;begintryConsole:=CoConsole.CreateInstance;// Get the WMI class path
p:=CoManagementPath.CreateInstance('\\ComputerName\root'+'\cimv2:Win32_LogicalDisk.DeviceID=''C:''');Console.WriteLine_15('IsClass: {0}',p.IsClass);// Should be False (because it is an instance)
Console.WriteLine_15('IsInstance: {0}',p.IsInstance);// Should be True
Console.WriteLine_14('ClassName: '+p.ClassName);// Should be "Win32_LogicalDisk"
Console.WriteLine_14('NamespacePath: '+p.NamespacePath);// Should be "ComputerName\cimv2"
Console.WriteLine_14('Server: '+p.Server);// Should be "ComputerName"
Console.WriteLine_14('Path: '+p.Path);// Should be "ComputerName\root\cimv2:
// Win32_LogicalDisk.DeviceId="C:""
Console.WriteLine_14('RelativePath: '+p.RelativePath);// Should be "Win32_LogicalDisk.DeviceID="C:""
exceptonE:ExceptiondobeginConsole.WriteLine_14(E.message);end;end;end.