Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Regist for USB device notification in windows?

Posted on 2011-03-03
5
Medium Priority
?
1,556 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 46

Expert Comment

by:Kent Olsen
ID: 35031327
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
ID: 35034358
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
ID: 35034450
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
ID: 35034607
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 2000 total points
ID: 35035238
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

Industry Leaders: 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

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

971 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