Click here to Skip to main content
15,881,715 members
Articles / Mobile Apps
Tip/Trick

Getting Processor architecture in x86 and x64 bit platform.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Oct 2010CPOL1 min read 43.4K   6   3
GetSystemInfo provides the basic system information and processor architecture of the underlying platform. This API can be used successfully in both x64 and x86 platform. But, under 64-bit WIndows, we can run 32 bit Applications( WOW64). If a WOW64 process call GetSystemInfo API, it will return the

GetSystemInfo provides the basic system information and processor architecture of the underlying platform. This API can be used successfully in both x64 and x86 platform. But, under 64-bit WIndows, we can run 32 bit Applications( WOW64). If a WOW64 process call GetSystemInfo API, it will return the processor architecture as x86. Of course it should be the way, this API act otherwise there could compatibility problems may arise and application could act weird and show undefined behavior.


If the WOW64 process want to know the original platform it’s running, it must call GetNativeSystemInfo. When we’ve to use this? I’ve a realworld example. When we spawn process explorer (procexp.exe, it realize the underlying platform and create another exe procexp64.exe (64bit version) to iterate all process information in the system. Note that the GetNativeSystemInfo need to be called only if you 32bit application wants to run under 64 bit platform and need to care about the true underlying platform. In all other cases, call GetSystemInfo, which works across platforms uniquely. See the snippet below


Code snippet – GetSystemInfo – x64 on Snipplr



C++
#include "stdafx.h"
#include <windows.h>

void displayPrcessorInfo( SYSTEM_INFO &stInfo ) 
{
	switch( stInfo.wProcessorArchitecture )
	{
	case PROCESSOR_ARCHITECTURE_INTEL:
		printf( "Processor Architecture: Intel x86\n");
		break;
	case PROCESSOR_ARCHITECTURE_IA64:
		printf( "Processor Type: Intel x64\n");
		break;
	case PROCESSOR_ARCHITECTURE_AMD64:
		printf( "Processor Type: AMD 64\n");
		break;
	default:
		printf( "Unknown processor architecture\n");
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	SYSTEM_INFO stInfo;
	GetSystemInfo( &stInfo );
	displayPrcessorInfo(stInfo);

	GetNativeSystemInfo( &stInfo );
	displayPrcessorInfo(stInfo);
	return 0;
}
</windows.h>


Alternatively, you can check the size of a pointer to determine whether you're running in 64-bit or 32-bit platform. But this method works similar to GetSystemInfo. If the process WOW64 process (32 bit on 64 bit OS, it will return the pointer size 4). It's not possible to detect the underlying platform using this method. For that we must call GetNativeSystemInfo API

<br />
if(sizeof(void*) == 4 )<br />
 // 32 bit<br />
else(sizeof(void*) == 8 )<br />
 // 64 bit<br />



To know more about the different processor architecture in abstract level, please do check one of my previous posts.

License

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


Written By
Technical Lead
India India
Software Developer

Comments and Discussions

 
BugNot a bug but.. Pin
tttony074-Feb-13 13:20
tttony074-Feb-13 13:20 
GeneralMy mistake. Did not read it properly. Yes, you said it right... Pin
Ajay Vijayvargiya13-Oct-10 20:30
Ajay Vijayvargiya13-Oct-10 20:30 
General<code>if(sizeof(void*) == 4 ) // 32 bit else(sizeof(void*) =... Pin
Ajay Vijayvargiya13-Oct-10 20:28
Ajay Vijayvargiya13-Oct-10 20:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.