Link to home
Start Free TrialLog in
Avatar of srimallikarthik
srimallikarthikFlag for India

asked on

How to Regist for USB device notification in windows?

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)
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

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


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.
Avatar of srimallikarthik

ASKER

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?
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?

ASKER CERTIFIED SOLUTION
Avatar of parnasso
parnasso
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial