How do you Detect 32-bit or 64-bit Compiler Versions?

Just starting out? Need help? Post your questions and find answers here.
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

How do you Detect 32-bit or 64-bit Compiler Versions?

Post by percy_b »

Greetings everyone and Happy New Year!

I have an application where I am using the same code base for the 32-bit version as well as the 64-bit version. However, the application needs to communicate with a PHP web page. The application needs to tell the PHP web page if it has been compiled in 32-bit mode or 64-bit mode. So, I am wondering if there is a compiler director that can detect which PureBasic compiler was used (i.e., 32-bit or 64-bit).

Does anyone know?
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
   Debug "32 bit"
CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
   Debug "64 bit"
CompilerEndIf
See https://www.purebasic.com/documentation/reference/compilerdirectives.html,
Section "Reserved Constants".
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by percy_b »

Wow, that was easy!

Thanks Little John!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Dude »

Related question: how can my exe know if it's running on (not compiled on) a 32-bit or 64-bit PC?

I had the following code which was supposed to be correct, but it reports 0 for me even though my PC is 64-bit. :(

Code: Select all

Global os64

fnIsWow64Process=GetProcAddress_(GetModuleHandle_("kernel32"),"IsWow64Process")
If fnIsWow64Process
  If Not CallFunctionFast(fnIsWow64Process,GetCurrentProcess_(),@os64)
  EndIf
EndIf

Debug os64 ; Returns 0 on my 64-bit Win 10 PC. :(
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

percy_b, you are welcome!
percy_b wrote:Wow, that was easy!
:)
Yes, PureBasic actually has a lot of built-in stuff.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

Dude wrote:Related question: how can my exe know if it's running on (not compiled on) a 32-bit or 64-bit PC?
Answer (Windows & Linux):

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
   Procedure.i OSBits()
      ; out: 32 or 64
      ;
      ; successfully tested on
      ; - Windows XP Professional 32 Bit SP3
      ; - Windows 7  Professional 64 Bit SP1 (with PB 5.31 32-Bit and 64-Bit)
      ; - Windows 10 Pro                     (with PB 5.40 32-Bit and 64-Bit)
      ; [after freak   <http://www.purebasic.fr/english/viewtopic.php?p=256372#p256372>
      ;  and   ts-soft <http://www.purebasic.fr/english/viewtopic.php?p=381691#p381691>]
      Protected si.SYSTEM_INFO, kernel32.i, ret.i=32
      
      kernel32 = OpenLibrary(#PB_Any, "kernel32.dll")
      If kernel32
         If GetFunction (kernel32, "GetNativeSystemInfo")
            CallFunction(kernel32, "GetNativeSystemInfo", @ si)
            If si\wProcessorArchitecture <> 0
               ret = 64
            EndIf
         EndIf
         CloseLibrary(kernel32)
      EndIf
      
      ProcedureReturn ret
   EndProcedure
   
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
   
   Procedure.i OSBits()
      ; out: 32 or 64 (0 on error)
      ;
      ; successfully tested on
      ; - Linux Mint 17.2 Cinnamon 64 Bit with PB 5.40 x64
      ; - Linux Mint 19.1 Cinnamon 64 Bit with PB 5.70 x64
      ; [after <http://stackoverflow.com/questions/246007/how-to-determine-whether-a-given-linux-is-32-bit-or-64-bit>]
      Protected prog.i, ret.i=0
      
      prog = RunProgram("getconf", "LONG_BIT", "", #PB_Program_Open | #PB_Program_Read)
      If prog
         ret = Val(ReadProgramString(prog))
         CloseProgram(prog)
      EndIf
      
      ProcedureReturn ret
   EndProcedure
CompilerEndIf


Debug OSBits()
Last edited by Little John on Fri Jan 04, 2019 3:58 pm, edited 1 time in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Dude »

Thanks, Little John! ;)
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by percy_b »

Yes, thanks Little John! This will also come in handy.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

I have added an OSBits() function for Linux to my previous message. :-)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Mijikai »

How about:

Code: Select all

Debug SizeOf(Integer)
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

Mijikai wrote:How about:

Code: Select all

Debug SizeOf(Integer)
Nice. :-)
That's a shorter version of this code.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Dude »

Code: Select all

Debug SizeOf(Integer)
Returns 4 for both 32-bit and 64-bit, so that's no good.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

Dude wrote:

Code: Select all

Debug SizeOf(Integer)
Returns 4 for both 32-bit and 64-bit
Not here (tested on Windows).

Compiled with PB 5.62 x86: Debug SizeOf(Integer) shows 4.
Compiled with PB 5.62 x64: Debug SizeOf(Integer) shows 8.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Josh »

Little John wrote:Not here (tested on Windows).

Compiled with PB 5.62 x86: Debug SizeOf(Integer) shows 4.
Compiled with PB 5.62 x64: Debug SizeOf(Integer) shows 8.
I think Dude was referring to that question:
Dude wrote:Related question: how can my exe know if it's running on (not compiled on) a 32-bit or 64-bit PC?
@Dude
It is not a good behavior to extend a thread with a question that has nothing to do with the original topic. It'll only lead to confusion like this.
sorry for my bad english
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How do you Detect 32-bit or 64-bit Compiler Versions?

Post by Little John »

Josh wrote:I think Dude was referring to that question:
Dude wrote:Related question: how can my exe know if it's running on (not compiled on) a 32-bit or 64-bit PC?
Maybe. However, Mijikai was not referring to that question, because SizeOf() works at compile time. Since I anticipated this possible misunderstanding, I deliberately commented on Mijikai's post:
That's a shorter version of this code.
Post Reply