Get USB disk drive letter by device path or handle

后端 未结 1 414
耶瑟儿~
耶瑟儿~ 2021-01-28 03:58

My goal is to write a c-dll (compiled with MinGW) that is able to search for certain models of USB sticks connected to the computer and deliver the serial number, the vendor ID,

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-28 04:53

    I found out what my problem was. From what I read on the internet it seems there where other people having the same problems like me, so I will post my solution.

    The whole point is that there are obviously different path values one can obtain for a USB device using the SetupApi. All path values can be used to get a handle to that device, but there are obviously differences about what can be done with the handle. My failure was to use GUID_DEVINTERFACE_USB_DEVICE to list the devices. I found out that when I use GUID_DEVINTERFACE_DISK, I get a different path value that lets me request the device number. That way I am able to get the link to the drive letter. That path value obtained with GUID_DEVINTERFACE_DISK also contains the serial number but not the vendor and product IDs. But since both path values do contain the serial, it is no problem to get them both and build the relation.

    I tested the code with Windows XP, 7 and 8 and it works fine. Only the FileCreate code of the code sample above must be adjusted (replace GENERIC_WRITE by 0). Otherwise Administrator rights or compatibility mode are required.

    I did not try to find out what these different GUID values really stand for. Someone with a deeper knowledge in this area could probably provide a better explanation.

    Best regards, Florian

    0 讨论(0)
提交回复
热议问题