CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    5

    How to Detect When an USB Device has been unplugged

    Hi,

    I am developing an application where I have to determine if an USB Connector assigned to a COM Port has been pulled out.

    The obvious approach which I tried was to detect the available COM Ports using SerialPort.GetPortNames(). Unfortunately, even after physically pulling it out, the Port Name list still shows the same port being present.
    Obviously, any operations would now fail as the connection is no longer available, but how to do this without making any communication, that is, without sending or receiving data.

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: How to Detect When an USB Device has been unplugged

    http://www.pinvoke.net/default.aspx/...ification.html

    should contain everything you need, or at least get you started.

  3. #3
    Join Date
    Jan 2010
    Posts
    5

    Re: How to Detect When an USB Device has been unplugged

    Thanks for the reply.

    That would be one way to do this task.

    However, one thing that I don't understand is why SerialPort.GetPortNames() will still show a COM that has been removed.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How to Detect When an USB Device has been unplugged

    The good old API route :

    RegisterDeviceNotification
    UnRegisterDeviceNotification

    As pointed out by mariocatch ( ) is the best way to go
    Last edited by HanneSThEGreaT; March 9th, 2010 at 08:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured