views:

655

answers:

1

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

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.

DanDan