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)
{
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)
{
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]