Determing 32 or 64 bit

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Determing 32 or 64 bit

Post by SFSxOI »

In there a way with Purebaic to determine if the OS is 32 bit or 64 bit?
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post by milan1612 »

This information is stored by Windows in this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Keyname: PROCESSOR_ARCHITECTURE
Windows 7 & PureBasic 4.4
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post 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...
Windows 7 & PureBasic 4.4
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Does it still return "AMD64" on intel chipsets and processors?

I cant really tell because i'm running 32 bit.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

SFSxOI wrote:Does it still return "AMD64" on intel chipsets and processors?
Yes! Is the same result on intel-cpu
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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 ;))
quidquid Latine dictum sit altum videtur
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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()
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

I would stay with the dynamic loading, as the program won't run on pre-XP Windows versions else.
quidquid Latine dictum sit altum videtur
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Nice examples everyone, thanks for the help. :)
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Post 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()
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Post by Arctic Fox »

With the 'new' PB 4.30 you can just use the #PB_Compiler_Processor constant :wink:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re:

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Demivec
Addict
Addict
Posts: 4269
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Re:

Post 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.
Post Reply