tags:

views:

650

answers:

5

Hi everyone, and to the makers of the site - thanks. looks like a promising beta.

I am trying to register to a "Device added/ Device removed" event using WMI. When I say device - I mean something in the lines of a Disk-On-Key or any other device that has files on it which I can access...

I am registering to the event, and the event is raised, but the EventType propery is different from the one I am expecting to see.

The documentation (MSDN) states : 1- config change, 2- Device added, 3-Device removed 4- Docking. For some reason I always get a value of 1.

Any ideas ?

Here's sample code :

public class WMIReceiveEvent
{
    public WMIReceiveEvent()
    {
        try
        {
            WqlEventQuery query = new WqlEventQuery(
                "SELECT * FROM Win32_DeviceChangeEvent");

            ManagementEventWatcher watcher = new ManagementEventWatcher(query);
            Console.WriteLine("Waiting for an event...");

            watcher.EventArrived += 
                new EventArrivedEventHandler(
                HandleEvent);

            // Start listening for events
            watcher.Start();

            // Do something while waiting for events
            System.Threading.Thread.Sleep(10000);

            // Stop listening for events
            watcher.Stop();
            return;
        }
        catch(ManagementException err)
        {
            MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
        }
    }

    private void HandleEvent(object sender,
        EventArrivedEventArgs e)
    {
        Console.WriteLine(e.NewEvent.GetPropertyValue["EventType"]);
    }

    public static void Main()
    {
        WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
        return;
    }
}
A: 

Oh! Yup, I've been through that, but using the raw Windows API calls some time ago, while developing an ActiveX control that detected the insertion of any kind of media. I'll try to unearth the code from my backups and see if I can tell you how I solved it. I'll subscribe to the RSS just in case somebody gets there first.

dguaraglia
A: 

Damn, I can't seem to find the source code. Will check in my old backups, because there's a lot of interesting code in my old projects... Update you when/if I find it.

dguaraglia
+1  A: 

Well, I couldn't find the code. Tried on my old RAC account, nothing. Nothing in my old backups. Go figure. But I tried to work out how I did it, and I think this is the correct sequence (I based a lot of it on this article):

  1. Get all drive letters and cache them.
  2. Wait for the WM_DEVICECHANGE message, and start a timer with a timeout of 1 second (this is done to avoid a lot of spurious WM_DEVICECHANGE messages that start as start as soon as you insert the USB key/other device and only end when the drive is "settled").
  3. Compare the drive letters with the old cache and detect the new ones.
  4. Get device information for those.

I know there are other methods, but that proved to be the only one that would work consistently in different versions of windows, and we needed that as my client used the ActiveX control on a webpage that uploaded images from any kind of device you inserted (I think they produced some kind of printing kiosk).

dguaraglia
A: 

Well,

u can try win32_logical disk class and bind it to the __Instancecreationevent. You can easily get the required info

A: 

I tried this on my system and I eventually get the right code. It just takes a while. I get a dozen or so events, and one of them is the device connect code.

JoelHess