C++读取硬盘物理序列号-非管理员权限

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int main()
{
    static BYTE buffer[0x1000];
    WCHAR path[] = L"\\\\.\\PhysicalDrive0";

    STORAGE_DEVICE_DESCRIPTOR* descriptor = (STORAGE_DEVICE_DESCRIPTOR*)buffer;
    STORAGE_PROPERTY_QUERY p = { StorageDeviceProperty, PropertyStandardQuery };
    DWORD retn;
    DWORD len = wcslen(path);
    while(true) {        
        HANDLE hDisk = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                               OPEN_EXISTING, 0, NULL);
        if (hDisk == INVALID_HANDLE_VALUE) break;

        if (!DeviceIoControl(hDisk, IOCTL_STORAGE_QUERY_PROPERTY, &p, sizeof(p),
            buffer, sizeof(buffer), &retn, 0)) break;
        
        wprintf(L"%s\n", path);

        if (descriptor->VendorIdOffset > 0)
            printf("VendorId: %s\n", buffer + descriptor->VendorIdOffset);

        if (descriptor->ProductIdOffset > 0)
            printf("ProductId: %s\n", buffer + descriptor->ProductIdOffset);

        if (descriptor->ProductRevisionOffset > 0)
            printf("ProductRevision: %s\n", buffer + descriptor->ProductRevisionOffset);

        if (descriptor->SerialNumberOffset > 0)
            printf("SerialNumber: %s\n", buffer + descriptor->SerialNumberOffset);

        printf("\n");
        CloseHandle(hDisk);
        path[len - 1]++;
    }

    return 0;
}
————————————————
版权声明:本文为CSDN博主「wuxq」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wuxq/java/article/details/89071444

posted @ 2020-05-17 17:02  袁氏家谱网  阅读(60)  评论(0编辑  收藏