Solved

How to detect USB device arrival and removal.

Posted on 2004-04-29
11
3,245 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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