Solved

How to Regist for USB device notification in windows?

Posted on 2011-03-03
5
1,457 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
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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

770 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