编译运行后,再打开 DiskGenius 磁盘工具 和 使用 wmic cpu get processorid 验证结果还算正确

#include <stdio.h>
#include <windows.h>
#include <cpuid.h>
int main()
{
    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
    __cpuid(1, eax, ebx, ecx, edx);
    printf("ProcessorId:  %08X%08X\n", edx, eax); // cpuid 格式同 wmic cpu get processorid
    printf("CPU Features: %08X-%08X-%08X-%08X\n", eax, ebx, ecx, edx);

    DWORD serial_number = 0;
    char file_system_name[MAX_PATH + 1] = "";
    if (GetVolumeInformation("C:\\", NULL, MAX_PATH + 1, &serial_number,
                             NULL, NULL, file_system_name, MAX_PATH + 1))
        printf("Hard Disk ID: %08X\t%s\n", serial_number, file_system_name);
    else
        perror("GetVolumeInformation");
}

使用 Windows API 获得UUID

#include <windows.h>
#include <stdio.h>
int main()
{
    UUID uuid;
    UuidCreate(&uuid);   // 使用 Windows API 中的 UuidCreate() 函数来生成 UUID

//  UUID: 07e4bca4-61f2-4485-aed2-9fea6de42d48    // 需要链接库 librpcrt4.a
    char *uuid_str;
    UuidToStringA(&uuid, (RPC_CSTR*)&uuid_str);   // 为 UUID 字符串分配动态内存
    printf("UUID: %s\n", uuid_str);
    RpcStringFreeA((RPC_CSTR*)&uuid_str);         // 释放 UUID 字符串动态内存
    return 0;
}

以上代码 使用 TDM-GCC 10.2 编译, MSVC 需要 VC2010以上

#include <stdio.h>
#include <intrin.h>  // MSVC 2010 使用 intrin.h 头文件和  __cpuid 函数

int main()
{
    int info[4];
    __cpuid(info, 1);  // 获取 CPU ID

    printf("ProcessorId:  %08X%08X\n", info[3], info[0]); // cpuid 格式同 wmic cpu get processorid
    printf("CPU Features: %08X-%08X-%08X-%08X\n", info[0], info[1], info[2], info[3]);
    return 0;
}

在 Microsoft Visual C++ (MSVC) 中,cpuid.h 头文件是在 MSVC 2010 版本中引入的。因此,如果您使用的是 MSVC 2010 及更高版本,应该可以使用 cpuid.h 头文件。

请注意,cpuid.h 只在 Windows 操作系统下可用,并且需要基于 x86 或 x64 架构的 CPU 才能使用 CPUID 指令。此外,cpuid.h 头文件是针对特定的编译器实现的,可能不会在其他编译器上工作。如果您尝试在不支持的编译器上使用 cpuid.h,可能会遇到编译错误或链接错误。

分类: 学习编程

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注

© 2024 兰雅VBA代码分享
浙ICP备2021017795号-2 浙公网安备33078102100266号