1

I am trying to implement an USB device notifier which has static events when e.g. a USB device is unpugged. I tried this with a class like the following:

public static class WmiUsbNotifier
{
    private static ManagementEventWatcher watcherDetach;

    public static event EventHandler<EventArgs<WmiDeviceNotifyEventArgs>> DeviceNotify;

    public static void Initialize()
    {
        var detachQuery = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'");
        watcherDetach = new ManagementEventWatcher();
        watcherDetach.EventArrived += ManagementEventWatcher_Detaching;
        watcherDetach.Query = detachQuery;
        watcherDetach.Start();
    }
    public static void ShutDown()
    {
        watcherDetach?.Stop();
        watcherDetach?.Dispose();
        watcherDetach = null;
    }

    private static void ManagementEventWatcher_Detaching(object sender, EventArrivedEventArgs e)
    {
        var targetInstance = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
        var id = targetInstance.Properties["Dependent"].Value.ToString();
        DeviceNotify?.Invoke(watcherDetach, new WmiDeviceNotifyEventArgs(EventType.DeviceRemoveComplete, id));
    }
}

This works fine and everything is well IF I call ShutDown() before closing my app (e.g. in OnExit of my WPF App). But if I do not call that I get the following error when closing my app:

An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in System.Management.dll

Additional information: COM object that has been separated from its underlying RCW cannot be used.

Now I would like to avoid this WITHOUT the need to "externally call ShutDown()", because this is part of a library which can be used by others and the chances are high the will forget to call that method in their apps.

I tried already calling it in AppDomain.CurrentDomain.DomainUnload, but the exception occures before that. I also tried making the class above not static and adding a finalizer/IDisposable and setting an instance as a static property of another class, but there the finalizer is also called to late.

Is there any way to call the ShutDown() method "automatically" before this exception occurs?
Or is there a way to catch and swallow exactly this exception? (As it happens when shutting down it can be ignored anyway)

4
  • Can you listen to the Application.Exit event by reflection? Nov 30, 2016 at 13:53
  • @sweerpotato: Isn't that only a WinForms event? What about Console/WPF/COM/...? But I will check... Nov 30, 2016 at 13:55
  • 1
    The fatal mistake is in the ManagementEventWatcher's finalizer, it calls Stop(). And Stop() uses ReleaseComObject(), kaboom when the COM object is already finalized. This bug is taking out Microsoft's own products as well. No simple fix here, the library user has to help. Consider not using System.Management. Nov 30, 2016 at 14:14
  • @HansPassant: Thanks for your input. I will try your solution. I previously used LibUSB.NET's device notifier with such a "invisible form", but had a case where it did not work... Nov 30, 2016 at 14:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.