Solved

How to detect USB device arrival and removal.

Posted on 2004-04-29
11
3,239 Views
Last Modified: 2013-12-03
Hi,

Could you please let me know how to detect USB device arrival. Am writing a windows 32 application using SDK which would detect arrival of any device at USB port. After detecting the device it would get device handle and then later to get details of the device using it's handle.

MSDN describes all top windows will recieve WM_DEVICECHANGE message with DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE; upon inserting and removing the device respetively into the PC. Unfortunately I do not recieve these messages/events whenever I insert a device or remove it. However, I could receive these messages whenever I inserted a CD_ROM. Does this mean that windows deliver these messages only when CD_ROM device is inserted into the disk?

Then I thought; I should be using RegisterDeviceNotification but the remarks on this API indicates that I need not register in order to recieve above events and more over no registration is required for volume type devices either.

I will be greatful to you if you could help me in resolving this most nagging problem.
0
Comment
Question by:vtatikonda
  • 5
  • 4
11 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 200 total points
ID: 10949958
_I_ received them OK - it's only the top level window that gets them, though.


See http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20970771.html as I've just asked a similar Q yesterday!
0
 

Author Comment

by:vtatikonda
ID: 10956302
andrewjb

Thank you for a very quick response. I need some time to write this code and test. Will be able to get back to you in couple of days time.

vtatikonda
0
 

Author Comment

by:vtatikonda
ID: 10960700
andrewjb

Your suggestion worked. Thanks a lot for guiding me. It has resolved first part of my problem. However I am accepting it as a solution as you have at least guided me to some point before I continue to struggle. If possible could you please tell me how to use PDEV_BROADCAST_DEVICEINTERFACE
{
DWORD dbcc_size;  DWORD dbcc_devicetype;  DWORD dbcc_reserved;  GUID dbcc_classguid;  TCHAR dbcc_name[1];
}

As I could only get the name of the device from the above structure. I know there should be something else through which I could get more details of device. Like to which drive the usb storage device is mapped etc...

In my application I am suppose to trap USB device arrival and get the details like, name of the device, drive to which it has been mapped, number of bytes copied from and to.

I could get these details if the device type is of volume; that is by using FirstDriveFromMask I am getting drive letter subsequentely device handle from the drive. Finally using device handle I am calling DeviceIOcontrol calls to get above details. Similarly is there any way to get while the device is of type PDEV_BROADCAST_DEVICEINTERFACE.

vtatikonda
0
 

Author Comment

by:vtatikonda
ID: 10960789
andrewjb

I am sorry; How to accept this one as solution without closing this question. I wanted to accept this but I saw I have to close this question by accepting. Which I do not want.

Anyway please let me know if know how to accept this without closing this question.

vtatikonda.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:andrewjb
ID: 10960807
Ah... the WM_DEVICECHANGE is only automatic for (COM) ports, not all devices, methinks.

And I've not progressed any further than this - I only wanted to know about COM port arrival. Sorry - I'd just have to trawl through the documentation, too.

Perhaps you should re-ask this question?
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 10960919
You can't accept but keep open. You can split points later on, and give points to multiple people once you're happy with the overall answer.

You might get more support by closing this one (reduce points if necssary - ask in Community support) then asking a new question with a more specific title and description now you've progressed a little further.
0
 

Author Comment

by:vtatikonda
ID: 10984437
andrewjb

I requested community expert to allocate 200 points to you and leave this question open. Thank you for your suggessions.

An update for you. May be it could be useful to you.

I am using SetupDI*** calls to retrieve device related information after recieving device arrival notification. However I am still unsuccessful in retriving complete data. Currently I am succeful in creating device handle but program is failing when I am using this handle to retrive device related information with API DeviceIOcontrol.

I continuing my work in this area. Please do let me know if you figure out something on USB related stuff.

Thanks,
vtatikonda.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 11222897
Venabili - see comment above yours. He was going to split...
0
 

Author Comment

by:vtatikonda
ID: 11229721
Venabili

Please alocate 200 points to andrewjb before you remove this question. Thank you.

vtatikonda.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now