Is there some function to know if the Windows is 32 or 64?
Marcos Douglas

Mailing lists / fpc-pascal / 2011-03
27 messages · 7 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Is there some function to know if the Windows is 32 or 64?
Marcos Douglas
Marcos Douglas wroteIs there some function to know if the Windows is 32 or 64?
You can use the GetNativeSystemInfo function. Check TSystemInfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64 to see if it's 64 bit.
Henry
What unit?
I've used this function in Delphi (7) and didn't work:
function is64bitOS: boolean;
var
SysInfo: TSystemInfo;
begin
GetSystemInfo(SysInfo);
Result := (
(Sysinfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64)
or (Sysinfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64)
);
end;Marcos Douglas
None, since the minimum supported client is XP. Try to load it yourself, e.g.:
type
TGNSIProc = procedure (var lpSystemInfo: TSystemInfo); stdcall;
const
PROCESSOR_ARCHITECTURE_AMD64 = 9;
function Isx64: Boolean;
var
si: TSystemInfo;
GNSIProc: TGNSIProc;
begin
Result := False;
ZeroMemory(@si, SizeOf(si));
@GNSIProc := GetProcAddress(GetModuleHandle('kernel32.dll'),
'GetNativeSystemInfo');
if @GNSIProc = nil then
Exit;
GNSIProc(si);
if si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64 then
Result := True;
end;This ignores ia64, as you can see.
Henry
I've used this in Delphi 32 and it seems to work. Forgot where I pulled the basic info. Probably doesn't do much on native 64 bit.
function Is64BitOS: Boolean;
type
TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) :
BOOL; stdcall;
var
hKernel32 : Integer;
IsWow64Process : TIsWow64Process;
IsWow64 : BOOL;
begin
// we can check if the operating system is 64-bit by checking whether
// we are running under Wow64 (we are 32-bit code). We must check if this
// function is implemented before we call it, because some older versions
// of kernel32.dll (eg. Windows 2000) don't know about it.
// see http://msdn.microsoft.com/en-us/library/ms684139%28VS.85%29.aspx
Result := False;
hKernel32 := LoadLibrary('kernel32.dll');
if (hKernel32 = 0) then RaiseLastOSError;
@IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process');
if Assigned(IsWow64Process) then
Begin
IsWow64 := False;
if (IsWow64Process(GetCurrentProcess, IsWow64)) then
Result := IsWow64
end;
FreeLibrary(hKernel32);
end;Jeff.
None, since the minimum supported client is XP. Try to load it yourself, e.g.: type…
Weird. This code did not compile in this line:
@GNSIProc := GetProcAddress(GetModuleHandle('kernel32.dll'),
'GetNativeSystemInfo');
Error: unit1.pas(46,2) Error: Can't assign values to an addressThis ignores ia64, as you can see.
What is ia64?
Marcos Douglas
Just a guess:
Add {$mode delphi} at the top.
Another solution would be:
GNSIProc := TGNSIProc(GetProcAddress(...));> > This ignores ia64, as you can see. What is ia64?
A 64-bit architecture developed by Intel and supported by Windows server systems (although it should be dropped with one of the next versions?!) and Linux systems.
Also see here:
http://en.wikipedia.org/wiki/IA64
Regards,
Sven
Weird. This code did not compile in this line: @GNSIProc :=…
This was Delphi code, so remove the @.
Henry
Just a guess: Add {$mode delphi} at the top. Another solution would be: GNSIProc :=…
I forgot... =( I just copied the code from Delphi to Lazarus... sorry. I used the second solution.
Right. But is better ignore the IA64?
I found this code to Delphi:
const
PROCESSOR_ARCHITECTURE_AMD64 = 9;
{$EXTERNALSYM PROCESSOR_ARCHITECTURE_AMD64}
PROCESSOR_ARCHITECTURE_IA64 = 6;
{$EXTERNALSYM PROCESSOR_ARCHITECTURE_IA64}
function GetNativeSystemInfo(var SystemInfo: TSystemInfo): Boolean;
type
TGetNativeSystemInfo = procedure (var SystemInfo: TSystemInfo) stdcall;
var
LibraryHandle: HMODULE;
_GetNativeSystemInfo: TGetNativeSystemInfo;
begin
Result := False;
LibraryHandle := GetModuleHandle(kernel32);
if LibraryHandle 0 then
begin
_GetNativeSystemInfo := GetProcAddress(
LibraryHandle, 'GetNativeSystemInfo');
if Assigned(_GetNativeSystemInfo) then
begin
_GetNativeSystemInfo(SystemInfo);
Result := True;
end
else
GetSystemInfo(SystemInfo);
end
else
GetSystemInfo(SystemInfo);
end;
function IsWindows64: Boolean;
var
ASystemInfo: TSystemInfo;
begin
GetNativeSystemInfo(ASystemInfo);
Result := ASystemInfo.wProcessorArchitecture in
[PROCESSOR_ARCHITECTURE_IA64, PROCESSOR_ARCHITECTURE_AMD64];
end;So, IA64 is 6. That is correct?
Marcos Douglas
Marcos Douglas wroteRight. But is better ignore the IA64?
You can do whatever you like. We chose to ignore it, since we don't support it. You just have to check for the
PROCESSOR_ARCHITECTURE_IA64, too, if you'd like to support it. See the msdn page:
http://msdn.microsoft.com/en-us/library/ms724958%28v=vs.85%29.aspx
Henry
OK, thanks.
Marcos Douglas
I've used this in Delphi 32 and it seems to work. Forgot where I pulled the basic info. …
Thanks Jeff, this also works and seems to be standard function to know if is a 32 or 64bits
See http://msdn.microsoft.com/en-us/library/ms684139%28v=vs.85%29.aspx
Marcos Douglas
In our previous episode, Marcos Douglas said:
IIRC Delphi.about.com has Delphi code for this.
But I need it for FPC.
Marcos Douglas
Thanks Jeff, this also works and seems to be standard function to know if is a 32 or…
Marcos Douglas wroteThanks Jeff, this also works and seems to be standard function to know if is a 32 or 64bits
No, this won't work if your application is compiled for win64 (since it won't run under the emulator), as Jeff mentioned.
Henry
You're right.
Look the parameter Wow64Process [out]:
"(...)If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE".
http://msdn.microsoft.com/en-us/library/ms684139%28v=vs.85%29.aspx
Thanks.
Marcos Douglas
My final test:
program iswin64;
{$mode objfpc}{$H+}
uses Classes, dynlibs, Windows;
const
PROCESSOR_ARCHITECTURE_AMD64 = 9;
PROCESSOR_ARCHITECTURE_IA64 = 6;
type
TGetNativeSystemInfo = procedure (var lpSystemInfo: TSystemInfo); stdcall;
function Is64bits: Boolean;
var
h: TLibHandle;
si: TSystemInfo;
getinfo: TGetNativeSystemInfo;
begin
Result := False;
H := GetModuleHandle('kernel32.dll');
try
ZeroMemory(@SI, SizeOf(SI));
getinfo := TGetNativeSystemInfo(GetProcAddress(h, 'GetNativeSystemInfo'));
if not Assigned(@getinfo) then
Exit;
getinfo(SI);
if SI.wProcessorArchitecture in
[PROCESSOR_ARCHITECTURE_AMD64, PROCESSOR_ARCHITECTURE_IA64] then
Result := True;
finally
UnloadLibrary(H);
end;
end;
begin
writeln('WIN 64 ', Is64bits);
readln;
end.Marcos Douglas
What unit? I've used this function in Delphi (7) and didn't work: function is64bitOS:…
Not sure this will work for you or not, but here is what I use:
Program OSType ;
Uses SysUtils;
Begin ;
WriteLn ;
{$IFDEF WIN32}
WriteLn ('This is a 32-bit version of Windows.') ;
{$ENDIF$}
{$IFDEF WIN64}
WriteLn ('This is a 64-bit version of Windows.') ;
{$ENDIF}
End.The value returned, however, depends on whether the 32-bit compiler of the 64-bit cross-compiler was used.
Interesting. But I think not works on my case. The compiler is 32-bits but Windows can vary.
Marcos Douglas
Marcos Douglas kirjoitti keskiviikko 16 maaliskuu 2011 13:18:41:
On Wed, Mar 16, 2011 at 12:56 AM, Robert Wolfe
I was also thinking to suggest the compile time IFDEFs. If the application is compiled for 32-bits then it can behave like a 32-bit application. No need to check what do you need the run-time info?
Regards,
Juha
The problem already been resolved (see the begin of this thread). The program was compiled in 32-bits but I have to know what kind of OS is running because my program needs to install somethings.
Marcos Douglas
Marcos Douglas wroteThe program was compiled in 32-bits but I have to know what kind of OS is running because my program needs to install somethings.
That's why I needed it too. In my case, my code is always 32 bit, but to install a smart card NULL driver, I needed to be able to adjust both the 32 bit registry and the 64 bit registry. Just adjusting the 32 bit registry wasn't enough. I also had to make sure the code was being ran as an administrator.
Jeff.
Exactly.
I also had to make sure the code was being ran as an administrator.
That's nice. Can you share the function?
Marcos Douglas
Marcos Douglas wrote> I also had to make sure the code was > being ran as an administrator. That's nice. Can you share the function?
Again, don't remember where I found this, Google or MSDN probably. Only tested in Delphi.
function IsAdmin: Boolean;
{ ------------------------------------------------------------- Returns a boolean indicating whether or not user has admin privileges. (Call only when running under NT.)
------------------------------------------------------------- }
var
hAccessToken : tHandle;
ptgGroups : pTokenGroups;
dwInfoBufferSize : DWORD;
psidAdministrators : PSID;
int : integer; // counter
blnResult : boolean; // return flag
const
SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY = (Value:
(0,0,0,0,0,5)); // ntifs
SECURITY_BUILTIN_DOMAIN_RID: DWORD = $00000020;
DOMAIN_ALIAS_RID_ADMINS: DWORD = $00000220;
DOMAIN_ALIAS_RID_USERS : DWORD = $00000221;
DOMAIN_ALIAS_RID_GUESTS: DWORD = $00000222;
DOMAIN_ALIAS_RID_POWER_: DWORD = $00000223;
begin
Result := False;
ptgGroups := nil;
blnResult := OpenThreadToken( GetCurrentThread, TOKEN_QUERY, True,
hAccessToken );
if ( not blnResult ) then
begin
if GetLastError = ERROR_NO_TOKEN then
blnResult := OpenProcessToken( GetCurrentProcess, TOKEN_QUERY,
hAccessToken );
end;
if ( blnResult ) then
try
GetMem(ptgGroups, 1024);
blnResult := GetTokenInformation( hAccessToken, TokenGroups, ptgGroups, 1024,
dwInfoBufferSize );
CloseHandle( hAccessToken );
if ( blnResult ) then
begin
AllocateAndInitializeSid( SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
psidAdministrators );
{$R-}
for int := 0 to ptgGroups.GroupCount - 1 do
if EqualSid( psidAdministrators, ptgGroups.Groups[ int ].Sid ) then
begin
Result := True;
Break;
end;
{$R+}
FreeSid( psidAdministrators );
end;
finally
FreeMem( ptgGroups );
end;
end;Thanks, I will test.
Marcos Douglas
On Wed, Mar 16, 2011 at 9:21 AM, Juha Manninen The problem already been resolved (see the…
So you are writing your own application installer then?
I wrote (done!) an installer to an old application that no have installer. Was my first ("real" or "in production") application using FPC and Lazarus.
This old application uses Oracle, BDE and don't have installer... and, for my client, this is a big problem...
I will talk about that in another thread.
Marcos Douglas