Page 1 of 2
How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 1:37 pm
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?
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 2:04 pm
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".
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 2:13 pm
by percy_b
Wow, that was easy!
Thanks Little John!
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 2:21 pm
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. :(
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 2:22 pm
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.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 2:32 pm
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()
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 2:36 pm
by Dude
Thanks, Little John!

Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 3:17 pm
by percy_b
Yes, thanks Little John! This will also come in handy.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 3:59 pm
by Little John
I have added an
OSBits() function for
Linux to my previous message.

Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 9:34 pm
by Mijikai
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Fri Jan 04, 2019 9:42 pm
by Little John
Nice.

That's a shorter version of
this code.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Sat Jan 05, 2019 3:06 am
by Dude
Returns 4 for both 32-bit and 64-bit, so that's no good.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Sat Jan 05, 2019 8:59 am
by Little John
Dude wrote:
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.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Sat Jan 05, 2019 9:04 am
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.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Posted: Sat Jan 05, 2019 9:12 am
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: