Detect system architecture in C

GetNativeSystemInfo – retrieves information about the current system to an application running under WOW64. If the function is called from a 64-bit application, it is equivalent to the GetSystemInfo function. You can use this function in all Windows versions, from Windows XP to Windows 8.1.

SYSTEM_INFO – contains information about the current computer system. This includes the architecture and type of the processor, the number of processors in the system, the page size, and other such information.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>

Number of Processors:

void GetNumberOfProcessors(){
  SYSTEM_INFO stInfo;
  GetNativeSystemInfo(&stInfo);
  printf("Number of processors: %ld\n", stInfo.dwNumberOfProcessors);
}

Processor Architecture:

void GetSystemArch(){
  SYSTEM_INFO stInfo;
  GetNativeSystemInfo(&stInfo);
  printf("Processor Architecture: ");
  switch(stInfo.wProcessorArchitecture){
    case PROCESSOR_ARCHITECTURE_INTEL:
    	printf("x86\n");
    	break;
    case PROCESSOR_ARCHITECTURE_AMD64:
    	printf("x64\n");
    	break;
    case PROCESSOR_ARCHITECTURE_IA64:
    	printf("Itanium\n");
    	break;
    default:
    	printf("Unknown\n");
    	break;
  }
}
Categories: C Tags: , , ,
%d bloggers like this: