Hello. How I can get information about Windows OS type? Is it 32bit or 64bit? How I can get this information programatically?
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.
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.
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
.
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
).
Thanks you all for our answers. I get information I needed :)
P.S. Sorry for bad question formulation