Hi
System.Management namespace currently doesn't support creating permanent
consumers. You would have to use COM Interop to implement
IWbemUnboundObjectSink. It??s much more complicated if you want to do
something with the event object as you??ll need to write a method to
convert a IWbemClassObject to a ManagementObject.
using System;
using System.Runtime.InteropServices;
namespace dotnetconsumer
{
[Guid("B6D22F58-6183-44a8-B2F0-E82D8A3D83E9"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IWbemUnboundObjectSink
{
}
// Since the .NET Framework interface and coclass have to behave as
// COM objects, we have to give them guids.
[Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D06")]
public interface IManagedInterface : IWbemUnboundObjectSink
{
int PrintHi(string name);
}
[Guid("C6659361-1625-4746-931C-36014B14667A")]
public class WmiConsumer : IManagedInterface
{
public int PrintHi(string name)
{
Console.WriteLine("Hello, {0}!", name);
return 33;
}
}
}
Here is the link about permanent event subscribe in unmanaged code.
Receiving Events At All Times
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
receiving_events_at_all_times.asp
Best regards,
Perter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Khoi Ha - 28 Jan 2005 15:19 GMT
Hi,
I had a purpose to use WMI for watching a registry value change. I have
tested the following code using a console app. I think if you would use
it a web application then permission issue could be a problem. Try this
out:
public void RegistryWatcher()
{
WqlEventQuery evQuery = new WqlEventQuery();
evQuery.EventClassName = "RegistryEvent";
evQuery.WithinInterval = new TimeSpan(0,0,0,10,0);
evQuery.QueryString= @"Select * from RegistryValueChangeEvent where
hive='HKEY_LOCAL_MACHINE' and KeyPath='Software\\FooSoft' and
ValueName='FooKey'";
try
{
using(ManagementEventWatcher evListener = new
ManagementEventWatcher())
{
evListener.Query = evQuery;
evListener.EventArrived += new
EventArrivedEventHandler(OnRegistryChange);
ManagementScope mgrScope = new ManagementScope();
mgrScope.Path.Server = "localhost";
mgrScope.Path.NamespacePath = "root\\default";
evListener.Scope = mgrScope;
evListener.Start();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void OnRegistryChange(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Changed");
}
}
Khoi Ha