Question

I made a console application which detects plugin and plugout events for all type of usb devices. but I wanted some filteration in it like I wanted to detect only webcams . This was done by using GUID class. The class for webcam is 'Image' class with GUID "{6bdd1fc5-810f-11d0-bec7-08002be2092f}" .The problem is that this 'Image' class is also used for scanners and I dont want to detect scanners.The code is given below:

static void Main(string[] args)
    {
        WqlEventQuery weqQuery = new WqlEventQuery();
        weqQuery.EventClassName = "__InstanceOperationEvent";
        weqQuery.WithinInterval = new TimeSpan(0, 0, 3);
        weqQuery.Condition = @"TargetInstance ISA 'Win32_PnPEntity'";

        ManagementEventWatcher m_mewWatcher = new ManagementEventWatcher(weqQuery);
        m_mewWatcher.EventArrived += new EventArrivedEventHandler(m_mewWatcher_EventArrived);

        m_mewWatcher.Start();
        Console.ReadLine();
    }

    static void m_mewWatcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        bool bUSBEvent = false;
        string deviceCaption = "";
        string deviceType = "";

        foreach (PropertyData pdData in e.NewEvent.Properties)
        {
            try
            {
                ManagementBaseObject mbo = (ManagementBaseObject)pdData.Value;
                if (mbo != null)
                {

                    foreach (PropertyData pdDataSub in mbo.Properties)
                    {

                        Console.WriteLine(pdDataSub.Name + " = " + pdDataSub.Value);
                        if (pdDataSub.Name == "Caption")
                        {
                            deviceCaption = pdDataSub.Value.ToString();
                        }
                        if (pdDataSub.Name == "ClassGuid" && pdDataSub.Value.ToString() == "{6bdd1fc5-810f-11d0-bec7-08002be2092f}")
                        {
                            bUSBEvent = true;
                            deviceType = "Image";
                        }
                    }
                    if (bUSBEvent)
                    {
                        if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
                        {
                            Console.WriteLine("A " + deviceType + " device " + deviceCaption + " was plugged in at " + DateTime.Now.ToString());
                        }
                        else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
                        {
                            Console.WriteLine("A " + deviceType + " device " + deviceCaption + " was plugged out at " + DateTime.Now.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

for references check this link

Was it helpful?

Solution

I waited but no body answered this question so, after seeing all properties of ManagementBaseObject I found that there is a property named Service which is different for scanners. In scanners the value of Service property is usbscan while in cameras it is usbvideo. eg. you can do something like this

if (mbo.Properties["Service"].Value.ToString() == "usbscan")
{
//then it means it is a scanner
}
else
{
//then it means it is a camera
}

note: The main question was that how can we differentiate between a scanner and a webcam because they both use same GUID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top