Solved

How to Regist for USB device notification in windows?

Posted on 2011-03-03
5
1,465 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
[X]
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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can someone explain the following statement in C? 4 32
send to option in chrome 11 34
Delete Temp on all PCs 7 43
Problem to Citrix 2 8
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

740 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