1. Technology

Is Your 32bit Delphi Applications Running On x86 (Win 32) OR x64 (Win 64)?

Programmatically Check If 32bit App Is Running on 32-bit or 64-bit Windows

By

While I do have Delphi XE 2 (supporting native x64 development) I am still compiling some of my applications as 32-bit applications (waiting for some 3rd party to move to x64).

Having my x86 (32-bit) application running on various Windows versions - I need a way to know if my 32 bit application is running on Windows x86 (32-bit) or x64 (64 bit).

IsWow64Process

The IsWow64Process API function present in some versions of kernel32.dll can be used to determine whether the specified (32-bit) process is running under WOW64. WOW64, provided with the operating system, is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows.

Depending on the Windows version your Delphi application is being run on, the IsWow64Process API might be (or not) present in the kernel32.dll.

To check if your 32-bit Delphi application is running under 32-bit or 64-bit Windows, you need to:

  1. Have your application be 32 bit application (of course)
  2. Dynamically load the kernel32.dll (if it exists)
  3. Check (using GetProcAddress) if it exposes the IsWow64Process function
  4. If so, get the result of IsWow64Process
And here's the code:
function Running32ON64: boolean;
type
  TIsWow64Process = function(Handle:THandle; var IsWow64 : boolean) : boolean; stdcall;
var
  hDLL : cardinal;
  IsWow64Process : TIsWow64Process;
begin
  result := false;
  hDLL := LoadLibrary('kernel32.dll');
  if (hDLL = 0) then Exit;
  try
    @IsWow64Process := GetProcAddress(hDLL, 'IsWow64Process');
    if Assigned(IsWow64Process) then IsWow64Process(GetCurrentProcess, result);
  finally
    FreeLibrary(hDLL);
  end;
end;
NOTE: If the process (your application returned by GetCurrentProcess) is a 64-bit application running under 64-bit Windows, the result would be FALSE.

SysUtils.TOSVersion in Delphi XE 2

If you have (at least) Delphi XE 2, you should better use the SysUtils.TOSVersion record as it exposes the Architecture property (TArchitecture enumeration) returning arIntelX86 (for 32 bit Windows) or arIntelX64 (for 64 bit Windows).

Delphi tips navigator:
» More Delphi TIPS
« ShellControls (TShellTreeView, TShellListView) In Delphi XE2

  1. About.com
  2. Technology
  3. Delphi
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi Tips 2011
  7. Is Your 32bit Delphi Applications Running On x86 (Win 32) OR x64 (Win 64)?

©2014 About.com. All rights reserved.