Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the cpu id from a c++ program. This is my target.

Here is my code.

C++
#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;
}





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
Posted
Updated 18-Jul-16 1:21am
Comments
Rajesh R Subramanian 18-Jul-16 5:36am    
Are you on Windows? What IDE are you using?
RickyMa Wiskerlian 18-Jul-16 5:37am    
Yes IDE.

I am now using dev-C++ but i will soon change to visual studio 2012

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");
}


[EDIT]
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;
}

[EDIT]
 
Share this answer
 
v2
Comments
RickyMa Wiskerlian 19-Jul-16 3:13am    
I cannot use your code.
RickyMa Wiskerlian 19-Jul-16 3:14am    
#include <iostream>
#include <string.h>
using namespace std;

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()
{
return 0;
}
Jochen Arndt 19-Jul-16 5:16am    
See my update solution.
You must call the function passing the required arguments.
RickyMa Wiskerlian 19-Jul-16 3:14am    
is it correct? again I just include iostream and string.h
RickyMa Wiskerlian 20-Jul-16 5:36am    
I still cannot run, is there any version of c++
Your code compiles (and executes) fine (I am using Visual Studio 2013 Express Edition and have just removed the stdafx.h include).
 
Share this answer
 
Comments
RickyMa Wiskerlian 18-Jul-16 11:21am    
I am using Visual Studio Express 2012 but it said memcpy is not defined

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900