Page 1 of 2

OSBits()

Posted: Sat May 26, 2012 11:03 am
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

Re: OSBits()

Posted: Sat May 26, 2012 11:10 am
by KJ67
Something like this?

Code: Select all

Macro IntegerInBits()
  (SizeOf(Integer) * 8)
EndMacro

Re: OSBits()

Posted: Sat May 26, 2012 11:22 am
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

Re: OSBits()

Posted: Sat May 26, 2012 12:46 pm
by gnozal
Little John wrote:Does this tell the size at compile time or at run time?
Compile time.

Re: OSBits()

Posted: Sat May 26, 2012 12:50 pm
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()

Re: OSBits()

Posted: Sat May 26, 2012 12:57 pm
by ts-soft
There is no test required for WinVersion before WinXP, is always 32-Bit :wink:

Re: OSBits()

Posted: Sat May 26, 2012 1:01 pm
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()

Re: OSBits()

Posted: Sat May 26, 2012 1:08 pm
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

Re: OSBits()

Posted: Sat May 26, 2012 1:14 pm
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.

Re: OSBits()

Posted: Sat May 26, 2012 1:20 pm
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!

Re: OSBits()

Posted: Sat May 26, 2012 1:28 pm
by Danilo
So optimized. Thank you, ts-soft!

Re: OSBits()

Posted: Sat May 26, 2012 2:30 pm
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

Re: OSBits()

Posted: Sat May 26, 2012 2:42 pm
by KJ67
I think that Ts-soft's code checks if the CPU can run 64-bit code.

Re: OSBits()

Posted: Sat May 26, 2012 2:56 pm
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()

Re: OSBits()

Posted: Sat May 26, 2012 2:57 pm
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'.