How do you Detect 32-bit or 64-bit Compiler Versions?
How do you Detect 32-bit or 64-bit Compiler Versions?
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?
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?
-
- 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?
Code: Select all
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
Debug "32 bit"
CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
Debug "64 bit"
CompilerEndIf
Section "Reserved Constants".
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Wow, that was easy!
Thanks Little John!
Thanks Little John!
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
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.
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. :(
-
- 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?
percy_b, you are welcome!
Yes, PureBasic actually has a lot of built-in stuff.
percy_b wrote:Wow, that was easy!
Yes, PureBasic actually has a lot of built-in stuff.
-
- 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?
Answer (Windows & Linux):Dude wrote:Related question: how can my exe know if it's running on (not compiled on) a 32-bit or 64-bit PC?
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.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Thanks, Little John!
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Yes, thanks Little John! This will also come in handy.
-
- 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?
I have added an OSBits() function for Linux to my previous message.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
How about:
Code: Select all
Debug SizeOf(Integer)
-
- 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?
Nice.Mijikai wrote:How about:Code: Select all
Debug SizeOf(Integer)
That's a shorter version of this code.
Re: How do you Detect 32-bit or 64-bit Compiler Versions?
Code: Select all
Debug SizeOf(Integer)
-
- 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?
Not here (tested on Windows).Dude wrote:Returns 4 for both 32-bit and 64-bitCode: Select all
Debug SizeOf(Integer)
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?
I think Dude was referring to that question: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.
@DudeDude wrote:Related question: how can my exe know if it's running on (not compiled on) a 32-bit or 64-bit PC?
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
-
- 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?
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: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?
That's a shorter version of this code.