Solved

How to Regist for USB device notification in windows?

Posted on 2011-03-03
5
1,447 Views
Last Modified: 2013-12-04
Experts,

I am writing an application in C language,
I am communication to a USB device using winusb driver,
I need to know the device UNPLUG / PLUG status to application
Please help me how to register for device notification for USB device.
Note: (its not MFC, i found few examples which are registering for notification to window which i can't do here as it is simple C console application )
( http://msdn.microsoft.com/en-us/library/aa363432%28v=vs.85%29.aspx)
0
Comment
Question by:srimallikarthik
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi srimallikarthik,

Here's a link to a short PDF.

  http://www.codemiles.com/download/file.php?id=719&rct=j

It explains quite well how the process works and has a sample method to detect the change to the USB port when a device is plugged in or removed.



Kent


0
 
LVL 4

Expert Comment

by:parnasso
Comment Utility
You can use the example you have found,  http://msdn.microsoft.com/en-us/library/aa363432%28v=vs.85%29.aspx with a few modifications, even if you are writing a console application.

The main idea is to create a hidden window instead of a visible window using the flag SW_HIDE in the CreateWindowEx API call . The messages of upcomming USBs are sent to the main message loop you see in your sample. Substitute output messages, delete calls to MoveWindow and so on.

Hope this is helpful to you.
0
 

Author Comment

by:srimallikarthik
Comment Utility
Yes, now with the example mentioned in msdn, i can register for notification .
Can you help me, how to read the device name which device connected ? ( it will be device descriptor in case of USB device i guess)

To make my requirment still clear:
consider i have two USB pendrives/mass storage devices connected,
As per the above example in msdn link and kents link ,as i am registering events for particular class of USB devices (like mass storage/Human Interface Device/RAW device..) i will get events for both the pendrive plug / unplug if i register to Mass storage class.

Now my requirement is, i am intersted in one particular device of that class which has vendor/product id/ device name
Where should i check / which apis to use to find out these information after device notification?
0
 

Author Comment

by:srimallikarthik
Comment Utility
parnasso,

My code is a dll which is written in c, not an console application !!
1)Still can i use the above msdn example of registering for windows?

Kent,
your link is nice, gained more info

link mentioning two menthods of registering
1)registering to window
2)registering to service

here in my case as i am writing my code in c and it is a dll, which method i should use?

0
 
LVL 4

Accepted Solution

by:
parnasso earned 500 total points
Comment Utility
Yes your dll can regiester to a window and receive device notification messages like the example you posted (remember remove the MFC/ATL code if you are not using them and hide the window to receive messages)

Prior to continue, you must check that you receive notifications in your dll hidden window by setting a breakpoint in:
case DBT_DEVICEARRIVAL:
//...

Open in new window


Once you receive events, you might parse the structures to get the device information.
Take a look at the pointer to a structure you are given:

PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;

Which is defined as the following>
typedef struct _DEV_BROADCAST_DEVICEINTERFACE {
    DWORD dbcc_size;
    DWORD dbcc_devicetype;
    DWORD dbcc_reserved;
    GUID dbcc_classguid;
    TCHAR dbcc_name[1];
} DEV_BROADCAST_DEVICEINTERFACE *PDEV_BROADCAST_DEVICEINTERFACE;

Open in new window


Fortunately, the field dbcc_name has a USB string, the vendor Id and product Id that you are looking for. So i guess you have everything you need.
An example of dbcc_name is:

\\?\USB#Vid_XXXX&Pid_YYYY#000BFBAB8B8EBC07#{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}

Where you care of:

    - \\?\USB: this is a USB device
    - Vid_XXXX&Pid_YYYY: Vid/Pid are VendorID and ProductID

If you need more information about the upcomming USB device, check the following registry key for friendly name and description:

\\HKLM\SYSTEM\CurrentControlSet\Enum\USB\Vid_XXXX&Pid_YYYY







0

Featured Post

Wish Marketing would stop bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now