How to determine the OS in use?
How to determine the OS in use?
Hello,
I'm looking for a way to determine wether my PB Application is running on Linux, Windows or Mac OS X. There is a function called "OSVersion()" but I don't know how to handle its returncode (it's -2 when running under Linux). Has anyone a hint on how to do things right?
I'm looking for a way to determine wether my PB Application is running on Linux, Windows or Mac OS X. There is a function called "OSVersion()" but I don't know how to handle its returncode (it's -2 when running under Linux). Has anyone a hint on how to do things right?
#PB_Compiler_Os has one of the following values:
Code: Select all
- #PB_OS_Windows : The compiler is running on Windows
- #PB_OS_Linux : The compiler is running on Linux
- #PB_OS_AmigaOS : The compiler is running on AmigaOS
- #PB_OS_MacOS : The compiler is running on Mac OS X
well, be carefull,
OSVersion() is a runtime function while #PB_Compiler_OS is only for the compiler host (your OS while compiling, not while running).
i think OSVersion() is better suited for your purpose.
f(ucking) manual said :
OSVersion() is a runtime function while #PB_Compiler_OS is only for the compiler host (your OS while compiling, not while running).
i think OSVersion() is better suited for your purpose.
f(ucking) manual said :
Syntax
Result = OSVersion()
Description
Returns the version of the operating system on which the program has been launched.
'Result' can be one of the following values:
#PB_OS_Windows_NT3_51
#PB_OS_Windows_95
#PB_OS_Windows_NT_4
#PB_OS_Windows_98
#PB_OS_Windows_ME
#PB_OS_Windows_2000
#PB_OS_Windows_XP
#PB_OS_Windows_Server_2003
#PB_OS_Windows_Vista
#PB_OS_Windows_Server_2008
#PB_OS_Windows_Future ; New Windows version (not existent when the program was written)
Example:
Select OSVersion()
Case #PB_OS_Windows_98
MessageRequester("Info", "Windows 98")
Case #PB_OS_Windows_2000
MessageRequester("Info", "Windows 2000")
Case #PB_OS_Windows_XP
MessageRequester("Info", "Windows XP")
Default
MessageRequester("Info", "Unsupported Windows version")
EndSelect
Note that the constant values are ordered by the time of each versions release, so teste like the following one can be used to catch all versions older or newer than a given one:
If OsVersion() < #PB_OS_Windows_2000
;
; All versions older than Windows 2000
;
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Well thanks for the info. I'll try your solution as well and see, wether it also works for detecting Linux or MacOS as the operating system in use.Flype wrote:well, be carefull,
OSVersion() is a runtime function while #PB_Compiler_OS is only for the compiler host (your OS while compiling, not while running).
i think OSVersion() is better suited for your purpose.
f(ucking) manual said :
Syntax
Result = OSVersion()
Description
Returns the version of the operating system on which the program has been launched.
'Result' can be one of the following values:
#PB_OS_Windows_NT3_51
#PB_OS_Windows_95
#PB_OS_Windows_NT_4
#PB_OS_Windows_98
#PB_OS_Windows_ME
#PB_OS_Windows_2000
#PB_OS_Windows_XP
#PB_OS_Windows_Server_2003
#PB_OS_Windows_Vista
#PB_OS_Windows_Server_2008
#PB_OS_Windows_Future ; New Windows version (not existent when the program was written)
Example:
Select OSVersion()
Case #PB_OS_Windows_98
MessageRequester("Info", "Windows 98")
Case #PB_OS_Windows_2000
MessageRequester("Info", "Windows 2000")
Case #PB_OS_Windows_XP
MessageRequester("Info", "Windows XP")
Default
MessageRequester("Info", "Unsupported Windows version")
EndSelect
Note that the constant values are ordered by the time of each versions release, so teste like the following one can be used to catch all versions older or newer than a given one:
If OsVersion() < #PB_OS_Windows_2000
;
; All versions older than Windows 2000
;
EndIf
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Agreed, as compiled windows code won't run on Linux or Mac at all, compile-time makes the most sense for the question asked. OSVersion() allows you to choose to call an API or not at runtime depending upon whether the host OS is going to support it.It's still my opinion that #PB_Compiler_OS is what you need
BERESHEIT
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
lodger didn't make his point really clear.
he said: "...wether my PB Application is running on Linux, Windows or Mac OS X."
well, when it's about running, the OSVersion() will be the right choice,
but... is it possible to run the same executable on win/linux/mac?
I don't think, at least not without strong limitations.
so his question may be wrong, and Tronds answer may be right.
but as Flype said, #PB_Compiler_OS is only functionable while compiling,
it can be used to chose between different parts of codes to compile for win or linux.
maybe lodger has a basic understanding problem with creating programs for the different platforms PB can run on.
he said: "...wether my PB Application is running on Linux, Windows or Mac OS X."
well, when it's about running, the OSVersion() will be the right choice,
but... is it possible to run the same executable on win/linux/mac?
I don't think, at least not without strong limitations.
so his question may be wrong, and Tronds answer may be right.
but as Flype said, #PB_Compiler_OS is only functionable while compiling,
it can be used to chose between different parts of codes to compile for win or linux.
maybe lodger has a basic understanding problem with creating programs for the different platforms PB can run on.
oh... and have a nice day.
actually, i think Trond is right (which is logical indeed).
and one question :
what's the output of OSVersion() on the others OS ?
on windows, we all know there are so much windows version...
but what are the diff vers. of a MacOS, and a LinuxOS ?
and one question :
what's the output of OSVersion() on the others OS ?
on windows, we all know there are so much windows version...
but what are the diff vers. of a MacOS, and a LinuxOS ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Well, I already have at least some experience in cross-platform development. But that was done in C and for console applications only (very boring interface stuff).
Anyway, the problem is the following, On Windows I have to do this to get a screen (for drawing sprites):
While under Linux, SDL already provides a window, so I can simply do:
If I do the Linux way on Windows, I get a runtime error. If I do the Windows way on Linux, I get the SDL screen *plus* an additional App Window (you may want try it out). So I simply want to know "yeah, this is Windows" or "this is Linux" in order to have one piece of code (not the binary!) that compiles and runs on both platforms (skip the OS X part for now). In C one would use precompiler directives (#define / #ifdef / #endif etc.) for that.
So I have to check at compile time for the platform the executable is being build for. The first solution (compile time) works perfectly, while the other (runtime through OSVersion()) doesn't (error message on Linux is: there is no screen - debugging shows that neither code branch is used).
So, after testing, I found out that this does do the trick:
Once again: thank you for being so kind to help me with this issue,
Anyway, the problem is the following, On Windows I have to do this to get a screen (for drawing sprites):
Code: Select all
If Not (OpenWindow(0, 0, 0, 640, 480, "PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered))
MessageRequester("Error", "Cannot initialize Window!", #PB_MessageRequester_Ok)
End
EndIf
If Not (OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0))
MessageRequester("Information", "Cannot initialize Screen!", 0)
End
EndIf
Code: Select all
If Not (OpenWindowedScreen(0, 0, 0, 640, 480, 0, 0, 0))
MessageRequester("Information", "Cannot initialize Screen!", 0)
End
EndIf
So I have to check at compile time for the platform the executable is being build for. The first solution (compile time) works perfectly, while the other (runtime through OSVersion()) doesn't (error message on Linux is: there is no screen - debugging shows that neither code branch is used).
So, after testing, I found out that this does do the trick:
Code: Select all
If (#PB_Compiler_OS = #PB_OS_Windows)
If Not (OpenWindow(0, 0, 0, 640, 480, "PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered))
MessageRequester("Error", "Cannot initialize Window!", #PB_MessageRequester_Ok)
End
EndIf
If Not (OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0))
MessageRequester("Information", "Cannot initialize Screen!", 0)
End
EndIf
EndIf
If (#PB_Compiler_OS = #PB_OS_Linux)
If Not (OpenWindowedScreen(0, 0, 0, 640, 480, 0, 0, 0))
MessageRequester("Information", "Cannot initialize Screen!", 0)
End
EndIf
EndIf
Last edited by lodger on Wed Jul 04, 2007 6:36 pm, edited 2 times in total.
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany