#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ntddscsi.h>
int main() {
const wchar_t* drivePath = L"\\\\.\\PhysicalDrive0"; // 设置为磁盘的路径,根据需要更改
HANDLE hDevice = CreateFileW(
drivePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
if (hDevice == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
std::cerr << "Error opening the device. Error code: " << error << std::endl;
return 1;
}
STORAGE_PROPERTY_QUERY storagePropertyQuery;
storagePropertyQuery.PropertyId = StorageDeviceProperty;
storagePropertyQuery.QueryType = PropertyStandardQuery;
STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader;
DWORD bytesReturned;
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&storagePropertyQuery,
sizeof(storagePropertyQuery),
&storageDescriptorHeader,
sizeof(storageDescriptorHeader),
&bytesReturned,
nullptr
))
{
// 根据返回的 STORAGE_DESCRIPTOR_HEADER 获取设备信息
DWORD bufferSize = storageDescriptorHeader.Size;
std::vector<char> buffer(bufferSize);
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&storagePropertyQuery,
sizeof(storagePropertyQuery),
buffer.data(),
bufferSize,
&bytesReturned,
nullptr
))
{
STORAGE_DEVICE_DESCRIPTOR* deviceDescriptor = reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(buffer.data());
// 序列号在设备描述符的后面,跳过 VendorId 和 ProductId
wchar_t* serialNumber = reinterpret_cast<wchar_t*>(buffer.data() + deviceDescriptor->SerialNumberOffset);
std::wcout << L"Serial Number: " << serialNumber << std::endl;
}
else {
DWORD error = GetLastError();
std::cerr << "Error getting storage query property. Error code: " << error << std::endl;
}
}
else {
DWORD error = GetLastError();
std::cerr << "Error getting storage query property header. Error code: " << error << std::endl;
}
CloseHandle(hDevice);
return 0;
}
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;
}
参考:
https://blog.csdn.net/zzy1448331580/article/details/124696129
标签:std,windows,storagePropertyQuery,STORAGE,serialnum,DWORD,磁盘,serial,buf From: https://www.cnblogs.com/rebrobot/p/17913235.html