OSBits()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

OSBits()

Post by Little John »

Hi all,

we already have the function OSVersion(). As an addition, a function named say OSBits() would be useful, which in these days would return 32 or 64, respectively.

Regards, Little John
Last edited by Little John on Sat May 26, 2012 11:27 am, edited 2 times in total.
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: OSBits()

Post by KJ67 »

Something like this?

Code: Select all

Macro IntegerInBits()
  (SizeOf(Integer) * 8)
EndMacro
The best preparation for tomorrow is doing your best today.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: OSBits()

Post by Little John »

Does this tell the size at compile time or at run time?
I want a function that returns the size at run time.
Unfortunately, I don't have a 64 bit system for testing.

Regards, Little John
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: OSBits()

Post by gnozal »

Little John wrote:Does this tell the size at compile time or at run time?
Compile time.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OSBits()

Post by Danilo »

Runtime, Windows OS:

Code: Select all

#PROCESSOR_ARCHITECTURE_INTEL = 0
#PROCESSOR_ARCHITECTURE_AMD64 = 9

;Import "kernel32.lib"
;    GetNativeSystemInfo(*si.SYSTEM_INFO)
;EndImport

Procedure GetSystemInfo(*si.SYSTEM_INFO)
    Protected kernel32.i
    If OSVersion() < #PB_OS_Windows_XP
        GetSystemInfo_(*si)
    Else
        kernel32 = OpenLibrary(#PB_Any,"kernel32.dll")
        If kernel32
            CallFunction(kernel32,"GetNativeSystemInfo",*si)
            CloseLibrary(kernel32)
        EndIf
    EndIf
EndProcedure

Procedure OSBits()
    Protected si.SYSTEM_INFO
    GetSystemInfo(si)
    If si\wProcessorArchitecture = #PROCESSOR_ARCHITECTURE_INTEL
        ProcedureReturn 32 ;#PB_Processor_x86
    ElseIf si\wProcessorArchitecture = #PROCESSOR_ARCHITECTURE_AMD64
        ProcedureReturn 64 ;#PB_Processor_x64
    EndIf
EndProcedure

Debug OSBits()
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: OSBits()

Post by ts-soft »

There is no test required for WinVersion before WinXP, is always 32-Bit :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
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OSBits()

Post by Danilo »

ts-soft wrote:There is no test required for WinVersion before WinXP, is always 32-Bit :wink:
The test for '< XP' is required for the function GetNativeSystemInfo_(), which
is available on WinXP+. GetSytemInfo_() returns 32bit OS if running in Wow64,
whereas GetNativeSystemInfo returns the correct value.

Same same but different:

Code: Select all

#PROCESSOR_ARCHITECTURE_INTEL = 0
#PROCESSOR_ARCHITECTURE_AMD64 = 9

Procedure OSBits()
    Protected si.SYSTEM_INFO, kernel32.i
    If OSVersion() < #PB_OS_Windows_XP
        ProcedureReturn 32 ;#PB_Processor_x86
    Else
        kernel32 = OpenLibrary(#PB_Any,"kernel32.dll")
        If kernel32
            CallFunction(kernel32,"GetNativeSystemInfo",@si)
            CloseLibrary(kernel32)
            If si\wProcessorArchitecture = #PROCESSOR_ARCHITECTURE_INTEL
                ProcedureReturn 32 ;#PB_Processor_x86
            ElseIf si\wProcessorArchitecture = #PROCESSOR_ARCHITECTURE_AMD64
                ProcedureReturn 64 ;#PB_Processor_x64
            EndIf
        EndIf
    EndIf
EndProcedure

Debug OSBits()
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: OSBits()

Post by ts-soft »

The result of the missing function on < XP is always 0 = 32-Bit!
See here: http://purebasic.fr/english/viewtopic.p ... 72#p256372
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
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OSBits()

Post by Danilo »

ts-soft wrote:The result of the missing function on < XP is always 0 = 32-Bit!
See here: http://purebasic.fr/english/viewtopic.p ... 72#p256372
What are you trying to say? :twisted:

Your code at the link contains:

Code: Select all

Import ""
  GetNativeSystemInfo(*info)
EndImport
...and does not work on Win9x.

My codes work on all >= 32bit Windows.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: OSBits()

Post by ts-soft »

My english is bad :cry:
Only this check is required (but doesn't make a difference)

Code: Select all

Procedure OSBits()
  Protected si.SYSTEM_INFO, result = 32
  Protected DLL = OpenLibrary(#PB_Any, "Kernel32.dll")
  If DLL
    If GetFunction(DLL,"GetNativeSystemInfo")
      CallFunction(DLL, "GetNativeSystemInfo", @si)
      If si\wProcessorArchitecture
         result = 64
      EndIf
    EndIf
    CloseLibrary(DLL)
  EndIf
  ProcedureReturn result
EndProcedure
Debug OSBits() 
My links should show the code by Freak

// edit
Code fixed!
Last edited by ts-soft on Sat May 26, 2012 3:25 pm, edited 2 times in total.
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
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OSBits()

Post by Danilo »

So optimized. Thank you, ts-soft!
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: OSBits()

Post by Little John »

Many thanks to all of you!

Danilo's first code is correct here (Windows XP Professional SP3 32 bit), as well as Freak's code to which ts-soft has posted the link. But ts-soft, your last "optimized" code wrongly returns 64 here.

Thanks again, and Happy Whitsun!

Little John
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: OSBits()

Post by KJ67 »

I think that Ts-soft's code checks if the CPU can run 64-bit code.
The best preparation for tomorrow is doing your best today.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: OSBits()

Post by gnozal »

KJ67 wrote:I think that Ts-soft's code checks if the CPU can run 64-bit code.
Yes, it should be

Code: Select all

#PROCESSOR_ARCHITECTURE_AMD64 = 9
Procedure OSBits()
  Protected si.SYSTEM_INFO, result = 32
  Protected DLL = OpenLibrary(#PB_Any, "Kernel32.dll")
  If DLL
    If GetFunction(DLL,"GetNativeSystemInfo")
      CallFunction(DLL, "GetNativeSystemInfo", @si)
      If si\wProcessorArchitecture = #PROCESSOR_ARCHITECTURE_AMD64
        ProcedureReturn 64
      EndIf
    EndIf
    CloseLibrary(DLL)
  EndIf
  ProcedureReturn result
EndProcedure

Debug OSBits()
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OSBits()

Post by Danilo »

ts-soft wrote:

Code: Select all

      If si\dwProcessorType
        result = 64
      EndIf
si\dwProcessorType:
dwProcessorType
An obsolete member that is retained for compatibility. Use the wProcessorArchitecture, wProcessorLevel, and wProcessorRevision members to determine the type of processor.

PROCESSOR_INTEL_386 (386)
PROCESSOR_INTEL_486 (486)
PROCESSOR_INTEL_PENTIUM (586)
PROCESSOR_INTEL_IA64 (2200)
PROCESSOR_AMD_X8664 (8664)
It's always true a.k.a. '>0'.
Post Reply