views:

866

answers:

4

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?

+3  A: 

You can check for the existence of and then call IsWow64Process. The linked MSDN page shows the required code.

Reed Copsey
+13  A: 

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.

RRUZ
Is this allowed by windows' UAC when running app has medium or low permissions?
loursonwinny
I answer to my own comment: Yes! because kernel32.dll is part of every Delphi process
loursonwinny
A: 

if sizeof(IntPtr) == 8 you're a 64-bit application on 64-bit Windows (edit: applies only to Delphi Prism)

else if IsWow64Process succeeds and returns true, you're a 32-bit application on 64-bit Windows

else you're on 32-bit Windows

Franci Penov
First part isn't applicable, since Delphi doesn't compile 64-bit applications.
Mason Wheeler
Not yet anyway :-)
Gerry
Doesn't Delphi Prism build .Net applications to IL? :-)
Franci Penov
Yes, but Delphi and Delphi Prism are two different languages. When someone just says "Delphi", they usually mean the native version.
Mason Wheeler
+4  A: 
PhiS