Drive Letter to Device Instance ID

放肆的年华 提交于 2019-12-01 05:59:15

问题


How do I get from a drive letter to a device instance ID?

My process starts with a device arrival message. I have been successful in getting the drive letter from the arrival message and in opening the dvd tray.

I have searched the various Setup API items; but I haven't found anything that gets me from a drive letter to a device instance ID.

A solution in C# or VB.NET would be ideal, but I'm willing to figure it out from any other language as long as I can see the API calls.

Thanks in advance...


回答1:


You cannot do it directly.

The link is to use STORAGE_DEVICE_NUMBER. You can use DeviceIoControl with IOCTL_STORAGE_GET_DEVICE_NUMBER on your device name to populate this structure. Put this value to one side.
You then need to get device infomation on your system using SetupDiGetClassDevs setting the GUIDS as approriate, indicicating the drives your are insterested in. Then enumerate through the devices using SetupDiEnumDeviceInfo. Then enumerate the interfaces using SetupDiEnumDeviceInterfaces and finally get the information using SetupDiGetDeviceInterfaceDetail. In this structure returned you can get a DevicePath you can use to get the STORAGE_DEVICE_NUMBER as above. Match this with the STORAGE_DEVICE_NUMBER from your drive letter, and you have now linked a driver letter to your structure. Phew! Inside this structure is a DevInst.




回答2:


i know it's late for you now but not for everybody ^^

I had the same need and this is main line of how I did it:

-You need a window to receive device arrival and removal (as you said)

-Then you create a DeviceNotificationFilter initiated to dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE

-Then in the message loop of your window you look for VM_DEVICECHANGE

-When u receive it if wParam == DBT_DEVICEARRIVAL, use the lParam to check if it is a DBT_DEVTYPE_VOLUME (i was getting the letter and the type of the drive here) or a DBT_DEVTYPE_DEVICEINTERFACE ( there you can use your wellcasted lParam to get the InstanceId from the input structure).

When you connect a drive your receive DEVINTERFACE first then the other. I only give the main line beacause i did this long time ago and i don't have the code here, and also I had found a lot of code pieces on the net (long time ago so there should be more now ^^^) maybe msdn give a full code example to do that now.

If you read this and need more informations, i'll reply or make a full documented answer if many need it.

Hope it will help some of you.



来源:https://stackoverflow.com/questions/1187241/drive-letter-to-device-instance-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!