?
Solved

USB NOTIFICATION

Posted on 2011-03-09
3
Medium Priority
?
672 Views
Last Modified: 2012-05-11
hi -- i'm using C#, VS2010, and .net 4.0. i've written an application that interfaces to a custom usb device (using a bulk protocol driver). it works fine if the device is plugged in before i open the application (i initialize theusb device protocol when i open the appication). what i would like to do is be able to plug in the device after the application is open. to do that, i would need to use an event with a delegate attached that would call the initialization. what i'm looking for is what event should i be posting for?
I use P/invoke to call the various methods in the usb device handler that was supplied to me as part of the device. i have access to the C code for the device handler. as near as i can tell, the device handler just wraps win32usb calls.
thanks,
0
Comment
Question by:jdcoburn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 35090419
I would use a WMI event watcher to detect an instance creation for a Win32_USBControllerDevice.

USB Port Insert / Remove detection using WMI
http://dotnetslackers.com/community/blogs/basharkokash/archive/2008/02/06/usb-port-insert-remove-detection-using-wmi.aspx


using System;
using System.IO;
using System.Management;
using System.Windows.Forms;

public partial class Form1 : Form
{
    // Used to subscribe to event notifications based on a specified event query.
    private ManagementEventWatcher _watcher = null;

    private StreamWriter _writer;

    private void Form1_Load(object sender, EventArgs e)
    {
        this.AddUSBHandler();
    }

    private void AddUSBHandler()
    {
        // Represents a WMI event query in WQL format (Windows Query Language)
        ManagementScope scope = new ManagementScope("root\\CIMV2");

        // Represents a scope (namespace)for management operations.		
        scope.Options.EnablePrivileges = true;

        try
        {
            WqlEventQuery query = new WqlEventQuery();

            query.EventClassName = "__InstanceCreationEvent";
            query.WithinInterval = new TimeSpan(0, 0, 3);
            query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
            
            _watcher = new ManagementEventWatcher(scope, query);

            //adds event handler that’s is fired when the insertion event occurs
            _watcher.EventArrived += new EventArrivedEventHandler(USBInseted);

            //run the watcher
            _watcher.Start();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

            if (_watcher != null)
                _watcher.Stop();
        }

        using (FileStream stream = new FileStream(@"C:\USB_Log.txt", FileMode.Append))
        {
            _writer = new StreamWriter(stream);
        }
    }

    public void USBInseted(object sender, EventArgs e)
    {
        _writer.WriteLine("USB device is inserted at " + DateTime.Now.ToString());
    }

    // Important note: you should stop the watcher before exiting the application or else an excption is thrown  
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (_watcher != null)
            _watcher.Stop();
    }

}

Open in new window

0
 

Author Closing Comment

by:jdcoburn
ID: 35090649
i haven't tried it yet, but it looks complete. the url reference is titled to what i am looking for.
0
 

Author Comment

by:jdcoburn
ID: 35107968
hi -- where does the syntax "query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'"; come from? i'd like to refine the query so it only detects my device adding or leaving the usb enumeration.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question