Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / January 2005

Tip: Looking for answers? Try searching our database.

WMI: HowTo implement permanet event subscriptions using C#? [RePos

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PolycomPerformance - 14 Jan 2005 02:15 GMT
Could someone point me to an example on how to do this?
I have found examples on temporary event subscription which uses the
ManagementEventWatcher namespace, but nothing on permanet event subscription.

Thanks in advance.

P.S. This is a repost. I have correctly registered with a no-spam alias email.
"Peter Huang" [MSFT] - 14 Jan 2005 07:15 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.