How I can get information about Windows OS type? Is it 32bit or 64bit? How I can get this information programatically?
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;
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
.
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.
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
).
for delphi XE+
Uses System.SysUtils
Function IsWin64Or32: string;
Begin
if Pos( '64-bit', TOSVersion.ToString ) > 0 then
Result := '64-bit'
Else
Result := '32-bit';
End;
Example
lbl1.Caption := IsWin64Or32;
function TForm2.Arch: string;
begin
if TOSVersion.Architecture=arIntelX86 then
Result := '32-bit' Else Result := '64-bit'
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With