System.Runtime.InteropServices.InvalidComObjectException

  AdmireMhlaba 11:29 10 Sep 2014
Locked

Good day Forum family, I'm getting a strange error when I use code that listens for USB event in my windows form application VS 2013. The thing that is puzzling about this is that, I have tested the code using a console application VS 2013 and it works perfectly fine. Below is the error that I'm getting. Kindly help me fix this if you are knowledgeable.

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.

.............................................................................................................................................................................

BELOW CONSOLE CODE IS WORKING?? But Raising Error in Windows Forms.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Management;

namespace UsbDection { class Program { static ManagementEventWatcher watchingObect = null; static WqlEventQuery watcherQuery; static ManagementScope scope; static void Main(string[] args) { scope = new ManagementScope("root\CIMV2"); scope.Options.EnablePrivileges = true;

        AddInsetUSBHandler();
        AddRemoveUSBHandler();

        for (; ; ) ;
    }

    public static void AddRemoveUSBHandler()
    {

        try
        {
            USBWatcherSetUp("__InstanceDeletionEvent");
            watchingObect.EventArrived += new EventArrivedEventHandler(USBRemoved);
            watchingObect.Start();

        }

        catch (Exception e)
        {

            Console.WriteLine(e.Message);
            if (watchingObect != null)
                watchingObect.Stop();

        }

    }

    static void AddInsetUSBHandler()
    {

        try
        {
            USBWatcherSetUp("__InstanceCreationEvent");
            watchingObect.EventArrived += new EventArrivedEventHandler(USBAdded);
            watchingObect.Start();

        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message);
            if (watchingObect != null)
                watchingObect.Stop();

        }

    }

    private static void USBWatcherSetUp(string eventType)
    {

        watcherQuery = new WqlEventQuery();
        watcherQuery.EventClassName = eventType;
        watcherQuery.WithinInterval = new TimeSpan(0, 0, 2);
        watcherQuery.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
        watchingObect = new ManagementEventWatcher(scope, watcherQuery);
    }

    public static void USBAdded(object sender, EventArgs e)
    {

        Console.WriteLine("A USB device inserted");

    }

    public static void USBRemoved(object sender, EventArgs e)
    {

        Console.WriteLine("A USB device removed");

    }
}

}

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

WPA2 Hack Latest News: How Secure is your Wi-Fi?

Photoshop CC 2018 released with new Curvature Pen and better brush tools

Best kids apps for iPhone & iPad

Comment utiliser Twitter ?