码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-05-17 17:58:56      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:share   val   amp   ati   dash   csdn   rop   sha   product   

#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

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

标签:share   val   amp   ati   dash   csdn   rop   sha   product   

原文地址:https://www.cnblogs.com/yuanscn/p/12905847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!