枚举系统中全部 PhysicalDrive

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

void EnumeratePhysicalDrives() {
    HANDLE hDrive;
    DWORD bytesReturned;
    char driveName[24];
    STORAGE_DEVICE_NUMBER deviceNumber;

    // 尝试打开每个可能的物理驱动器
    for (int i = 0; i < 16; i++) {
        // 构造物理驱动器的名称
        sprintf_s(driveName, "\\\\.\\PhysicalDrive%d", i);

        // 尝试打开物理驱动器
        hDrive = CreateFileA(driveName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if (hDrive == INVALID_HANDLE_VALUE) {
            // 如果无法打开驱动器,可能是因为驱动器不存在
            continue;
        }

        // 尝试获取设备编号
        if (!DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &deviceNumber, sizeof(deviceNumber), &bytesReturned, NULL)) {
            std::cout << "Failed to get device number for " << driveName << std::endl;
        }
        else {
            std::cout << "Found Physical Drive: " << driveName << ", Device Type: " << deviceNumber.DeviceType << ", Device Number: " << deviceNumber.DeviceNumber << std::endl;
        }

        CloseHandle(hDrive);
    }
}

int main() {
    EnumeratePhysicalDrives();
    return 0;
}