Search code examples
delphi64-bit

How can I tell if I'm running on x64?


I just got a bug report for an issue that only occurs when the program is running "on a 64-bit machine." Now, Delphi doesn't produce 64-bit code, so theoretically that shouldn't matter, but apparently it does in this case. I think I have a workaround, but it will break things on 32-bit Windows, so I need some way to tell:

  1. If I'm running on a x64 or an x86 processor and
  2. If I'm running under a 64-bit version of Windows under Win32 emulation or native Win32 on a 32-bit OS.

Does anyone know how I can get those answers from within my app?


Solution

  • Mason, you can use IsWow64Process (WOW64 is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows)

    Uses Windows;
    
    type
      WinIsWow64 = function( Handle: THandle; var Iret: BOOL ): Windows.BOOL; stdcall;
    
    
    function IAmIn64Bits: Boolean;
    var
      HandleTo64BitsProcess: WinIsWow64;
      Iret                 : Windows.BOOL;
    begin
      Result := False;
      HandleTo64BitsProcess := GetProcAddress(GetModuleHandle('kernel32.dll'), 'IsWow64Process');
      if Assigned(HandleTo64BitsProcess) then
      begin
        if not HandleTo64BitsProcess(GetCurrentProcess, Iret) then
        Raise Exception.Create('Invalid handle');
        Result := Iret;
      end;
    end;
    

    Bye.