Hello There, Guest!
View New Posts  |  View Today's Posts
C/C++ - Check if program is running x86/x64

  • 0 Vote(s) - 0 Average


02-13-2016, 07:59 PM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

C/C++ - Check if program is running x86/x64
Code:
// CS = 0x23 (35) = x86 mode
// CS = 0x33 (51) = x64 mode

UINT16 __declspec(naked) WINAPI CS()
{
  __asm
  {
    MOV AX, CS
    RET
  }
}

#define IsX86() (CS() == 0x23)
#define IsX64() (CS() == 0x33)

Here's a simple method you can use to see if the program is running in x86 or x64 mode that I wrote. For every x86 & x64 process on 64bit Windows there are two allocated code segments. My code checks the CS register and this is probably one of the easiest methods to use. :)
This post was last modified: 02-14-2016, 01:49 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

02-15-2016, 10:08 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: C/C++ - Check if program is running x86/x64
Almost similarly, you can check if the program is running under Wow64 emulation in the following way:
Code:
#include <Windows.h>
#include <stdio.h>

BOOL __declspec(naked) WINAPI IsWow64(void)
{
  __asm
  {
    MOV EAX, FS:[0xC0] ; wow64cpu!X86SwitchTo64BitMode
    SHR EAX, 30
    RET
  }
}

int main(void)
{
  printf("IsWow64: %s\n", IsWow64() ? "True" : "False");
  return 0;
}

wow64cpu!X86SwitchTo64BitMode is a jmp to wow64cpu!CpupReturnFromSimulatedCode for Wow64. For people who don't want to use inline assembly, there's also the C/C++ intrinsic __readfsdword for the FS register from <intrin.h>..
This post was last modified: 02-15-2016, 10:49 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:



Users browsing this thread: 1 Guest(s)