检查 PhysicalDrive 类型

具体的类型定义在【参考1】:

typedef enum _STORAGE_BUS_TYPE {
  BusTypeUnknown = 0x00,
  BusTypeScsi,
  BusTypeAtapi,
  BusTypeAta,
  BusType1394,
  BusTypeSsa,
  BusTypeFibre,
  BusTypeUsb,
  BusTypeRAID,
  BusTypeiScsi,
  BusTypeSas,
  BusTypeSata,
  BusTypeSd,
  BusTypeMmc,
  BusTypeVirtual,
  BusTypeFileBackedVirtual,
  BusTypeSpaces,
  BusTypeNvme,
  BusTypeSCM,
  BusTypeUfs,
  BusTypeNvmeof,
  BusTypeMax,
  BusTypeMaxReserved = 0x7F
} STORAGE_BUS_TYPE, *PSTORAGE_BUS_TYPE;

示例代码:

#include <windows.h>
#include <iostream>
#include <winioctl.h>

void QueryDriveInterfaceType(int driveNumber) {
    HANDLE hDrive;
    char drivePath[256];
    sprintf_s(drivePath, "\\\\.\\PhysicalDrive%d", driveNumber);

    // 打开物理驱动器
    hDrive = CreateFileA(drivePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (hDrive == INVALID_HANDLE_VALUE) {
        std::cerr << "Unable to open " << drivePath << std::endl;
        return;
    }

    // 准备查询
    STORAGE_PROPERTY_QUERY query;
    memset(&query, 0, sizeof(query));
    query.PropertyId = StorageDeviceProperty;
    query.QueryType = PropertyStandardQuery;

    // 接收数据的缓冲区
    BYTE buffer[1024];
    DWORD bytesRead;

    // 查询设备属性
    BOOL result = DeviceIoControl(hDrive, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query),
                                  &buffer, sizeof(buffer), &bytesRead, NULL);
    if (result) {
        STORAGE_DEVICE_DESCRIPTOR* deviceDescriptor = (STORAGE_DEVICE_DESCRIPTOR*)buffer;
        switch (deviceDescriptor->BusType) {
            case BusTypeScsi:
                std::cout << drivePath << " is SCSI" << std::endl;
                break;
            case BusTypeAtapi:
                std::cout << drivePath << " is ATAPI" << std::endl;
                break;
            case BusTypeAta:
                std::cout << drivePath << " is ATA" << std::endl;
                break;
            case BusTypeSata:
                std::cout << drivePath << " is SATA" << std::endl;
                break;
            // 添加其他需要的接口类型
            default:
                std::cout << drivePath << " has an unknown interface type: " << (int)deviceDescriptor->BusType << std::endl;
                break;
        }
    } else {
        std::cerr << "Failed to query storage properties for " << drivePath << std::endl;
    }

    CloseHandle(hDrive);
}

int main() {
    // 示例:查询PhysicalDrive0的接口类型
    QueryDriveInterfaceType(0);
    return 0;
}

参考:

1.https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ne-winioctl-storage_bus_type