通过C ++获取cpuid序列号 [英] Get cpuid serial number by C++

查看:1631
本文介绍了通过C ++获取cpuid序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从c ++程序中获取cpu id。这是我的目标。



这是我的代码。



I want to get the cpu id from a c++ program. This is my target.

Here is my code.

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

int main(){
    int regs[4] = {0};
    char vendor[13];
    __cpuid(regs, 0);              // mov eax,0; cpuid
    memcpy(vendor, ®s[1], 4);   // copy EBX
    memcpy(vendor+4, ®s[2], 4); // copy ECX
    memcpy(vendor+8, ®s[3], 4); // copy EDX
    vendor[12] = '\0';
    printf("My CPU is a %s\n", vendor);
    return 0;
}











但它给了我一些错误,这是

1. stdafx.h:没有这样的文件或目录。

2. intrin.h:没有这样的文件或目录。



我尝试了什么:



我找到了一些链接



http://stackoverflow.com/questions/6491566/getting-the -machine-serial-number-and-cpu-id-using-cc-in-linux

http://stackoverflow.com/questions/21642347/cpu-id-using-c-windows

http://stackoverflow.com/questions/5658975/c-get-processor-id

http://stackoverflow.com/questions/38323203/c-cpu- id-prosser-id-running-in-any-os-linux-window?noredirect = 1#comment64061561_38323203

https://www.youtube.com/results?search_query=+check+cpu+ id + c%2B%2B

http://stackoverflow.com/questions/23103801/link-error-when-using-the-cpuid-in-intrin-h

https://social.msdn.microsoft.com/Forums/vstudio/en-US/b80068ac-e17b-4f21-85fd-1d87fdc9b6b6/link-error-when-using-the-cpuid-in-intrinh?forum= msbuild

https://msdn.microsoft.com/en-us/library/ms724381(v=vs.85).aspx

http s://msdn.microsoft.com/en-us/library/ms724958(v = vs.85).aspx

http://stackoverflow.com/questions/38323203/c-cpu- id-prosser-id-running-in-any-os-linux-window






But it gave me some errors, which are
1. stdafx.h: No such file or directory.
2. intrin.h: No such file or directory.

What I have tried:

I have found some links

http://stackoverflow.com/questions/6491566/getting-the-machine-serial-number-and-cpu-id-using-c-c-in-linux
http://stackoverflow.com/questions/21642347/cpu-id-using-c-windows
http://stackoverflow.com/questions/5658975/c-get-processor-id
http://stackoverflow.com/questions/38323203/c-cpu-id-prosser-id-running-in-any-os-linux-window?noredirect=1#comment64061561_38323203
https://www.youtube.com/results?search_query=+check+cpu+id+c%2B%2B
http://stackoverflow.com/questions/23103801/link-error-when-using-the-cpuid-in-intrin-h
https://social.msdn.microsoft.com/Forums/vstudio/en-US/b80068ac-e17b-4f21-85fd-1d87fdc9b6b6/link-error-when-using-the-cpuid-in-intrinh?forum=msbuild
https://msdn.microsoft.com/en-us/library/ms724381(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/ms724958(v=vs.85).aspx
http://stackoverflow.com/questions/38323203/c-cpu-id-prosser-id-running-in-any-os-linux-window

推荐答案

您发布的代码仅适用于Visual Studio中的Microsoft编译器因为它使用Microsoft特定的头文件 intrin.h (和 stdafx.h )和MS编译器特定的__cpuid,__ cpuidex [ ^ ]内在函数。



如果使用其他编译器,则必须检查是否提供了对x86的支持cpuid指令。如果没有,它可以使用内联汇编实现,它再次依赖于使用的编译器(如果支持和语法)。



以Linux为例,有一个定义头文件 arch / x86 / include / asm / processor.h 当使用与Gnu编译器相同的内联汇编语法的编译器时,也可以与Windows一起使用:

Your posted code will work only with the Microsoft compiler which is part of Visual Studio because it uses the Microsoft specific header file intrin.h (and stdafx.h) and the MS compiler specific __cpuid, __cpuidex[^] intrinsic function.

If you use another compiler, you must check if that provides its own support of the x86 cpuid instruction. If not, it may be implemented using inline assembly which again depends on the used compiler (if supported and syntax).

With Linux for example, there is a definition in the header file arch/x86/include/asm/processor.h which can be also used with Windows when using a compiler that uses the same inline assembly syntax like the Gnu compilers:
 static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
                 unsigned int *ecx, unsigned int *edx)
{
    /* ecx is often an input as well as an output. */
    asm volatile("cpuid"
        : "=a" (*eax),
        "=b" (*ebx),
        "=c" (*ecx),
        "=d" (*edx)
        : "0" (*eax), "2" (*ecx)
        : "memory");
}







GCC编译器的工作示例(使用Ubuntu测试) 14.04LTS):




A working example for the GCC compiler (tested with Ubuntu 14.04LTS):

#include <stdio.h>
#include <string.h>

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
                 unsigned int *ecx, unsigned int *edx)
{
    /* ecx is often an input as well as an output. */
    asm volatile("cpuid"
        : "=a" (*eax),
        "=b" (*ebx),
        "=c" (*ecx),
        "=d" (*edx)
        : "0" (*eax), "2" (*ecx)
        : "memory");
}

int main()
{
    int eax, ebx, ecx, edx;
    eax = 0;
    native_cpuid(&eax, &ebx, &ecx, &edx);
    printf("EAX: %08X EBX: %08X ECX: %08X EDX: %08X\n", eax, ebx, ecx, edx);
    char vendor[13];
    memcpy(vendor, &ebx, 4);
    memcpy(vendor+4, &edx, 4);
    memcpy(vendor+8, &ecx, 4);
    vendor[12] = '\0';
    printf("%s\n", vendor);
    return 0;
}





您的代码编译(并执行)正常(我使用 Visual Studio 2013 Express Edition 并刚刚删除 stdafx.h include)。
Your code compiles (and executes) fine (I am using Visual Studio 2013 Express Edition and have just removed the stdafx.h include).


这篇关于通过C ++获取cpuid序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆