Solved

USB NOTIFICATION

Posted on 2011-03-09
3
669 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

729 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