2

I want to know whether device is connected via USB (meaning it is a removable hard drive) or SATA (meaning it is an internal hard drive). That is how I get a list of devices

SP_DEVINFO_DATA volumeData;
volumeData.cbSize = sizeof (SP_DEVINFO_DATA);
SP_DEVICE_INTERFACE_DATA volumeInterfaceData;
volumeInterfaceData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
wchar_t buffer[1024];
PSP_DEVICE_INTERFACE_DETAIL_DATA volumeInterfaceDetail;
volumeInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
volumeInterfaceDetail->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
for (int j = 0; SetupDiEnumDeviceInterfaces (hVolumeInfo, 0, &GUID_DEVINTERFACE_VOLUME, j, &volumeInterfaceData); j++) {
    DWORD bufferPathSize = offsetof (SP_DEVICE_INTERFACE_DETAIL_DATA, DevicePath) + sizeof(TCHAR);
    SetupDiGetDeviceInterfaceDetail (
        hVolumeInfo,
        &volumeInterfaceData,
        volumeInterfaceDetail,
        bufferPathSize,
        &bufferPathSize,
        &volumeData
        ));
    <some actions here>
}

After such operations I get a kind of the following result for each connected volume:

volumeInterfaceDetail->DevicePath: "\\\\?\\storage#volume#{3ec3ba03-2789-11e4-8251-806e6f6e6963}#0000000015f00000#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}"

How can I detect an interface (USB, SATA) with which the considering device is connected? Or is there any other way to differ external and internal HDDs using WinAPI?

Ganya
  • 25
  • 1
  • 6

2 Answers2

3

You need to do the following:

  1. Use CreateFile to get a handle on to the device.
  2. Use DeviceIoControl to send an IOCTL_STORAGE_QUERY_PROPERTY ioctl to the device to ask it to tell you its properties.
  3. The resulting STORAGE_DEVICE_DESCRIPTOR structure contains a BusType enumeration that tells you the bus to which it is attached.

There's a small code snippet at the bottom of this page that you can use to get started.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61
  • 1
    I don't see any code snippets in the last page you linked (except for a single function call). However, I did find [this helpful example](http://codexpert.ro/blog/2013/10/26/get-physical-drive-serial-number-part-1/). – Jeff Irwin Jan 10 '18 at 18:41
  • You're right @Jeff Irwin, Microsoft have edited the page and removed the example code. – Andy Brown Jan 11 '18 at 10:23
0

Looks like GetDriveType is just the thing to establish the drive type.


If you're really interested in USB versus non-USB instead of whether the drive is removable, then the documentation of GetDriveType states, up-front:

To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331