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
Solved

How to Regist for USB device notification in windows?

Posted on 2011-03-03
5
1,460 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: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 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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

790 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