views:

200

answers:

6

Hello. How I can get information about Windows OS type? Is it 32bit or 64bit? How I can get this information programatically?

+2  A: 

If a) you're on windows and b) you can access the registry then HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion should be informative.

glenatron
Sorry, but no registry. I am using windows
gedO
@gedO: You are contradicting yourself. Windows means registry. Try running "regedit" from the start menu.
Brian
+1  A: 

I don't know how to call Win32 function in Delphi.

But if you write a 32-bit program, you can call the Win32 API IsWow64Process to know if you are in a 64-bit OS.

Of course, if you write a 64-bit exe, it will only run on 64-bit Windows, so there is no need to ask.

Timores
Good luck writing a 64-bit exe with Delphi. ;)
Deltics
+6  A: 
function IsWin64: Boolean;
var
  IsWow64Process : function(hProcess : THandle; var Wow64Process : BOOL): BOOL; stdcall;
  Wow64Process : BOOL;
begin
  Result := False;
  IsWow64Process := GetProcAddress(GetModuleHandle(Kernel32), 'IsWow64Process');
  if Assigned(IsWow64Process) then begin
    if IsWow64Process(GetCurrentProcess, Wow64Process) then begin
      Result := Wow64Process;
    end;
  end;
end;
Serg
+3  A: 

You need to use GetProcAddress() to check the availability of the IsWow64Process() function at runtime, like so:

function Is64BitWindows: boolean;
type
  TIsWow64Process = function(hProcess: THandle; var Wow64Process: BOOL): BOOL;
    stdcall;
var
  DLLHandle: THandle;
  pIsWow64Process: TIsWow64Process;
  IsWow64: BOOL;
begin
  Result := False;
  DllHandle := LoadLibrary('kernel32.dll');
  if DLLHandle <> 0 then begin
    pIsWow64Process := GetProcAddress(DLLHandle, 'IsWow64Process');
    Result := Assigned(pIsWow64Process)
      and pIsWow64Process(GetCurrentProcess, IsWow64) and IsWow64;
    FreeLibrary(DLLHandle);
  end;
end;

because that function is only available on Windows versions that do have a 64 bit flavour. Declaring it as external would prevent your application from running on Windows 2000 or Windows XP pre SP2.

Edit:

Chris has posted a comment about caching the result for performance reasons. This may not be necessary for this particular API function, because kernel32.dll will always be there (and I can't imagine a program that would even load without it), but for other functions things may be different. So here's a version that caches the function result:

function Is64BitWindows: boolean;
type
  TIsWow64Process = function(hProcess: THandle; var Wow64Process: BOOL): BOOL;
    stdcall;
var
  DLLHandle: THandle;
  pIsWow64Process: TIsWow64Process;
const
  WasCalled: BOOL = False;
  IsWow64: BOOL = False;
begin
  if not WasCalled then begin
    DllHandle := LoadLibrary('kernel32.dll');
    if DLLHandle <> 0 then begin
      pIsWow64Process := GetProcAddress(DLLHandle, 'IsWow64Process');
      if Assigned(pIsWow64Process) then
        pIsWow64Process(GetCurrentProcess, IsWow64);
      WasCalled := True;
      FreeLibrary(DLLHandle);
    end;
  end;
  Result := IsWow64;
end;

Caching this function result is safe, as the API function will either be there or not, and its result can't change on the same Windows installation. It is even safe to call this concurrently from multiple threads, as two threads finding WasCalled to be False will both call the function, write the same result to the same memory location, and only afterwards set WasCalled to True.

mghie
It will be a good idea to cache the result if the program needs to know the answer multiple times. i.e. you wouldn't want to call this in a loop, as LoadLibrary will be expensive, particularly when it doesn't find anything. I'm not knocking the solution, just giving advice on the usage case.
Chris Thornton
This code will only compile if assignable/writable constants are enabled in the project settings. To avoid being sensitive to this compiler setting, either introduce directives to ensure this compiler behaviour is set (and restored) as required around this code, or better yet, I'd suggest using a unit variable for the cached result (using an integer avoids needing to use two such variables: e.g. declare initialised = -1 for "not set", set = 0 for Win32 set = 1 for Win64).
Deltics
A: 

In addition to IsWow64Process, the GetNativeSystemInfo API function may be of interest to you (it's defined in the Windows unit) to find out more about the CPU you're on (or you can use assembly and CPUID).

PhiS
A: 

Thanks you all for our answers. I get information I needed :)

P.S. Sorry for bad question formulation

gedO