Jump to content

How To Detect Windows 64bit, with Delphi code

- - - - -

  • Please log in to reply
No replies to this topic

#1
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
As 64 bit processors is getting more and more popular, 64 bit operating system is also becoming more popular. Well, you need 64 bit operating systems to get full advantage of the sophisticated 64bit processors. No wonder that more and more people also chose to install 64 bit operating system. On the other hand, for most programs switching from 32bit to 64bit does not give significant benefit over the hassle in converting process. Given that 32 bit programs run just fine under 64bit, and still get slight improvement in speed and throughput, makes converting to 64 bit is not really an urgent matter.

But no matter how good the 64bit OS is handling 32bit process, there is still some difference from true 32bit OS. And this is a must to keep the system running stable and clean. For Windows for example, this includes transparent registry and directory redirection. Thus making us have to be able to detect whether we are running under 64bit OS or not to anticipate this difference which maybe significantly affect our programs.

This article will discuss about how to detect if our program/process is running under Windows 64bit OS. The theory could be used in any programming language, but sample implementation will be done with Delphi.

The Theory
To check if the operating system is 64-bit we can use windows API IsWow64Process. This API is contained in kernel32.dll. Initially this api was not available in kernel32.dll of Windows 32 bit. So in those Windows simply detecting the presence of this api in kernel32.dll was enough for detection. Unfortunately in newer Windows versions (perhaps in Vista and 7, Microsoft was a bit vague on this) have IsWow64Process in their kernel32.dll, even in their 32 bit versions. This means now we have to actually run the api against our process for detection.

The possibility that IsWow64Process is missing in kernel32.dll is making static linking out of the question. We need dynamic linking or our application will not be able to run under Windows which missing IsWow64Process in their kernel32.dll.

Things also get slightly more complicated by the fact that some older Windows (like Windows 2000) does not have kernel32.dll. Therefore initial checking for the presence of this library is required.

So our steps should be:

  • Assume that we are not running under Windows 64bit.
  • Load kernel32.dll library, if failed simply return since we are running under older Windows which does not have kernel32.dll, which means it's definitely not 64 bit.
  • Load windows api IsWow64Process, if failed we simply jus return since we are definitely running under Windows 32 bit.
  • Execute IsWow64Process against our own process, and use the returned value to see if we are a 32 bit process running under Windows 64 bit OS.
For details on IsWow64Process you can visit: IsWow64Process Function (Windows)

Delphi Implementation


function IsWindows64: Boolean;

type

  TIsWow64Process = function(AHandle:THandle; var AIsWow64: BOOL): BOOL; stdcall;

var

  vKernel32Handle: DWORD;

  vIsWow64Process: TIsWow64Process;

  vIsWow64       : BOOL;

begin

  // 1) assume that we are not running under Windows 64 bit

  Result := False;


  // 2) Load kernel32.dll library

  vKernel32Handle := LoadLibrary('kernel32.dll');

  if (vKernel32Handle = 0) then Exit; // Loading kernel32.dll was failed, just return


  try


    // 3) Load windows api IsWow64Process

    @vIsWow64Process := GetProcAddress(vKernel32Handle, 'IsWow64Process');

    if not Assigned(vIsWow64Process) then Exit; // Loading IsWow64Process was failed, just return


    // 4) Execute IsWow64Process against our own process

    vIsWow64 := False;

    if (vIsWow64Process(GetCurrentProcess, vIsWow64)) then

      Result := vIsWow64;   // use the returned value


  finally

    FreeLibrary(vKernel32Handle);  // unload the library

  end;

end;


Edited by LuthfiHakim, 11 November 2010 - 09:32 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users