std::string get_disk_smart_serials(int index)
{
std::string ret;
char physical[256]{ 0 };
sprintf_s(physical, 256, "\\\\.\\PhysicalDrive%d", index);
HANDLE disk = CreateFileA(physical, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (disk != INVALID_HANDLE_VALUE)
{
STORAGE_PROPERTY_QUERY query{ };
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
const int b_size = 1024 * 10;
char* buf = new char[b_size];
if (buf)
{
memset(buf, 0, b_size);
DWORD r = 0;
BOOL status = DeviceIoControl(disk, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), buf, b_size, &r, NULL);
if (status)
{
STORAGE_DEVICE_DESCRIPTOR* descrip = (STORAGE_DEVICE_DESCRIPTOR*)buf;
if (descrip->SerialNumberOffset > 0)
{
std::string serial = buf + descrip->SerialNumberOffset;
serial.erase(std::remove_if(serial.begin(), serial.end(), [](unsigned char x) {return std::isspace(x); }), serial.end());
ret = serial;
}
}
delete[] buf;
}
CloseHandle(disk);
}
return ret;
}
|