|
| 1 | +#include "GetPhysicalDrives.h" |
| 2 | +#include <winioctl.h> |
| 3 | + |
| 4 | + |
| 5 | +#if !(_WIN32_WINNT >= 0x0601) |
| 6 | +#define BusTypeiScsi 0x9 |
| 7 | +#define BusTypeSas 0xA |
| 8 | +#define BusTypeSata 0xB |
| 9 | +#define BusTypeSd 0xC |
| 10 | +#define BusTypeMmc 0xD |
| 11 | +#endif |
| 12 | + |
| 13 | +#if !(_WIN32_WINNT >= 0x0601) |
| 14 | +#define BusTypeVirtual 0xE |
| 15 | +#define BusTypeFileBackedVirtual 0xF |
| 16 | +#endif |
| 17 | + |
| 18 | +#ifndef BusTypeNVMe |
| 19 | +#define BusTypeNVMe 17 |
| 20 | +#endif |
| 21 | +#ifndef BusTypeSCM |
| 22 | +#define BusTypeSCM 18 |
| 23 | +#endif |
| 24 | +#pragma GCC diagnostic push |
| 25 | +#pragma GCC diagnostic ignored "-Wswitch" |
| 26 | +std::string GetBusTypeString(STORAGE_BUS_TYPE busType) { |
| 27 | + switch (busType) { |
| 28 | + case BusTypeScsi: return "SCSI"; |
| 29 | + case BusTypeAtapi: return "ATAPI"; |
| 30 | + case BusTypeAta: return "ATA"; |
| 31 | + case BusType1394: return "FireWire"; |
| 32 | + case BusTypeSsa: return "SSA"; |
| 33 | + case BusTypeFibre: return "Fibre Channel"; |
| 34 | + case BusTypeUsb: return "USB"; |
| 35 | + case BusTypeRAID: return "RAID"; |
| 36 | + case BusTypeiScsi: return "iSCSI"; |
| 37 | + case BusTypeSas: return "SAS"; |
| 38 | + case BusTypeSata: return "SATA"; |
| 39 | + case BusTypeSd: return "SD"; |
| 40 | + case BusTypeMmc: return "MMC"; |
| 41 | + case BusTypeVirtual: return "Virtual"; |
| 42 | + case BusTypeNVMe: return "NVMe"; |
| 43 | + case BusTypeSCM: return "SCM"; |
| 44 | + case BusTypeUnknown: |
| 45 | + default: return "Unknown"; |
| 46 | + } |
| 47 | +} |
| 48 | +#pragma GCC diagnostic pop |
| 49 | + |
| 50 | + |
| 51 | +std::vector<DiskInfo> GetPhysicalDrives() { |
| 52 | + std::vector<DiskInfo> drives; |
| 53 | + |
| 54 | + for (int i = 0; i < 25; i++) { // Máximo 10 discos físicos |
| 55 | + std::string devicePath = "\\\\.\\PhysicalDrive" + std::to_string(i); |
| 56 | + HANDLE hDevice = CreateFileA(devicePath.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 57 | + NULL, OPEN_EXISTING, 0, NULL); |
| 58 | + |
| 59 | + if (hDevice == INVALID_HANDLE_VALUE) { |
| 60 | + continue; // NotExist |
| 61 | + } |
| 62 | + |
| 63 | + char buffer[512] = {0}; |
| 64 | + STORAGE_PROPERTY_QUERY query = {StorageDeviceProperty, PropertyStandardQuery}; |
| 65 | + DWORD bytesReturned = 0; |
| 66 | + |
| 67 | + if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), |
| 68 | + buffer, sizeof(buffer), &bytesReturned, NULL)) { |
| 69 | + STORAGE_DEVICE_DESCRIPTOR *desc = (STORAGE_DEVICE_DESCRIPTOR *)buffer; |
| 70 | + std::string siz(std::to_string(desc->Size)); |
| 71 | + std::string caption = "Unknown"; |
| 72 | + std::string bus = GetBusTypeString(desc->BusType); |
| 73 | + if (desc->ProductIdOffset) { |
| 74 | + caption = (buffer + desc->ProductIdOffset); |
| 75 | + } |
| 76 | + drives.push_back({devicePath, caption, bus}); |
| 77 | + } |
| 78 | + |
| 79 | + CloseHandle(hDevice); |
| 80 | + } |
| 81 | + |
| 82 | + return drives; |
| 83 | +} |
0 commit comments