Page 1 of 2

Determing 32 or 64 bit

Posted: Sun Aug 24, 2008 10:07 pm
by SFSxOI
In there a way with Purebaic to determine if the OS is 32 bit or 64 bit?

Posted: Sun Aug 24, 2008 10:43 pm
by milan1612
This information is stored by Windows in this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Keyname: PROCESSOR_ARCHITECTURE

Posted: Sun Aug 24, 2008 10:53 pm
by ts-soft
milan1612 wrote:This information is stored by Windows in this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Keyname: PROCESSOR_ARCHITECTURE
This isn't the right way to go :wink:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
In 32-Bit PB Programs come "x86" and in 64-Bit PB-Programs come "AMD64"
:wink:

Posted: Sun Aug 24, 2008 10:58 pm
by milan1612
ts-soft wrote:
milan1612 wrote:This information is stored by Windows in this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Keyname: PROCESSOR_ARCHITECTURE
This isn't the right way to go :wink:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
In 32-Bit PB Programs come "x86" and in 64-Bit PB-Programs come "AMD64"
:wink:
It is more or less the same - all environment variables are stored in this registry key :wink:
Your way is easier though...

Posted: Sun Aug 24, 2008 11:04 pm
by ts-soft
My way is wrong, the value was changed for emulated 32-bit application,
but in the registry is the right one, i think.

// edit
tested with PB 4.20

Code: Select all

EnableExplicit

Procedure.s Reg_ReadString(topKey.l, KeyName.s, ValueName.s)
  Protected Result.s, hKey.l, lpData.s = Space(255), lpcbData.l, Error.l
  lpcbData = (255 * SizeOf(Character)) + 1

  If Right(KeyName, 1) = "\" : KeyName = Left(KeyName, Len(KeyName) - 1) : EndIf
  Error = RegOpenKeyEx_(topKey, @KeyName, 0, #KEY_ALL_ACCESS, @hKey)
  If Error = #ERROR_SUCCESS
    Error = RegQueryValueEx_(hKey, @ValueName, 0, 0, @lpData, @lpcbData)
    If Error = #ERROR_SUCCESS
      Result = lpData
    Else
      lpData.s = Space(lpcbData)
      Error = RegQueryValueEx_(hKey, @ValueName, 0, 0, @lpData, @lpcbData)
      If Error = #ERROR_SUCCESS
        Result = lpData
      Else
        Result = ""
       EndIf
    EndIf
  EndIf
  If hKey : RegCloseKey_(hKey) : EndIf
  ProcedureReturn Result
EndProcedure

Debug Reg_ReadString(#HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "PROCESSOR_ARCHITECTURE")

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
Run with Adminprivilege
The first Debug gives "AMD64" and the second "x86" in 32-Bit programs
on 64-bit OS

Posted: Sun Aug 24, 2008 11:54 pm
by SFSxOI
Does it still return "AMD64" on intel chipsets and processors?

I cant really tell because i'm running 32 bit.

Posted: Mon Aug 25, 2008 12:08 am
by ts-soft
SFSxOI wrote:Does it still return "AMD64" on intel chipsets and processors?
Yes! Is the same result on intel-cpu

Posted: Mon Aug 25, 2008 12:34 am
by freak
The PROCESSOR_ARCHITECTURE environment variable is changed for 32 bit programs.
There is another variable PROCESSOR_ARCHITEW6432 present when running in WOW64 (32bit app on 64bit windows)

See here: http://blogs.msdn.com/david.wang/archiv ... tness.aspx

I would use the GetNativeSystemInfo() API function. Its only XP and newer, but these are the only versions with 64bit anyway, so if the function is not present, just assume 32bit.

This works without spechial priviledges:

Code: Select all

Prototype GetNativeSystemInfo(*lpSystemInfo.SYSTEM_INFO)

#PROCESSOR_ARCHITECTURE_AMD64 = 9
#PROCESSOR_ARCHITECTURE_IA64  = 6
#PROCESSOR_ARCHITECTURE_INTEL = 0
#PROCESSOR_ARCHITECTURE_UNKNOWN = $ffff


Procedure Is64bitOS()
  Protected kernel32, GetNativeSystemInfo.GetNativeSystemInfo
  Protected info.SYSTEM_INFO
  Protected Result = #False
  
  kernel32 = OpenLibrary(#PB_Any, "Kernel32.dll")
  If kernel32
    GetNativeSystemInfo = GetFunction(kernel32, "GetNativeSystemInfo")
    
    If GetNativeSystemInfo
      GetNativeSystemInfo(@info)
      If info\wProcessorArchitecture <> #PROCESSOR_ARCHITECTURE_INTEL ; x86
        Result = #True
      EndIf      
    EndIf
  
    CloseLibrary(kernel32)
  EndIf  

  ProcedureReturn Result
EndProcedure

Debug Is64bitOS()
(also works with PB64bit ;))

Posted: Mon Aug 25, 2008 1:21 am
by ts-soft
Thanks freak :D
Shortversion of freaks code:

Code: Select all

Import ""
  GetNativeSystemInfo(*info)
EndImport

Procedure Is64bitOS()
  Protected Info.SYSTEM_INFO
  GetNativeSystemInfo(Info)
  If info\wProcessorArchitecture
    ProcedureReturn #True
  EndIf 
EndProcedure

Debug Is64bitOS()

Posted: Mon Aug 25, 2008 2:43 am
by freak
I would stay with the dynamic loading, as the program won't run on pre-XP Windows versions else.

Posted: Mon Aug 25, 2008 12:42 pm
by SFSxOI
Nice examples everyone, thanks for the help. :)

Posted: Thu May 28, 2009 1:22 pm
by doctorized
SFSxOI wrote:Nice examples everyone, thanks for the help. :)
You can also try this:

Code: Select all

Procedure.l IsWin64() ;Tell us if the OS is x64 or not.
*Is64 = 0
If OpenLibrary(0,"KERNEL32.DLL")
	*F = GetFunction(0,"IsWow64Process")
	If *F
		CallFunctionFast(*F,GetCurrentProcess_(), @*Is64)
	EndIf
	CloseLibrary(0)
EndIf
	If *Is64>0
		ProcedureReturn 1 ; True
	Else
		ProcedureReturn 0 ; False
	EndIf
EndProcedure

Debug IsWin64()

Posted: Thu May 28, 2009 2:10 pm
by Arctic Fox
With the 'new' PB 4.30 you can just use the #PB_Compiler_Processor constant :wink:

Re:

Posted: Tue Dec 17, 2013 3:20 pm
by PB
ts-soft wrote:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
In 32-Bit PB Programs come "x86" and in 64-Bit PB-Programs come "AMD64"
On my 64-bit PC, it returns "x86" because I use the 32-bit version of PureBasic.
Just something to be aware of... it's therefore not reliable to use for testing.

Re: Re:

Posted: Tue Dec 17, 2013 4:03 pm
by Demivec
PB wrote:
ts-soft wrote:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
In 32-Bit PB Programs come "x86" and in 64-Bit PB-Programs come "AMD64"
On my 64-bit PC, it returns "x86" because I use the 32-bit version of PureBasic.
Just something to be aware of... it's therefore not reliable to use for testing.
ts-soft posted the same thing if you look at the quote.

freak's example and other's after him show correct results.