Page 1 of 1

How to determine the OS in use?

Posted: Wed Jul 04, 2007 11:12 am
by lodger
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?

Posted: Wed Jul 04, 2007 11:20 am
by Trond
#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

Posted: Wed Jul 04, 2007 11:26 am
by lodger
Thank you very much, that's just what I've been searching for ... :)

Posted: Wed Jul 04, 2007 12:01 pm
by Flype
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

Posted: Wed Jul 04, 2007 2:08 pm
by lodger
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
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.

Posted: Wed Jul 04, 2007 2:12 pm
by Trond
It's still my opinion that #PB_Compiler_OS is what you need.

Posted: Wed Jul 04, 2007 2:30 pm
by netmaestro
It's still my opinion that #PB_Compiler_OS is what you need
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.

Posted: Wed Jul 04, 2007 2:36 pm
by Kaeru Gaman
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.

Posted: Wed Jul 04, 2007 5:45 pm
by Flype
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 ?

Posted: Wed Jul 04, 2007 6:12 pm
by lodger
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):

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
While under Linux, SDL already provides a window, so I can simply do:

Code: Select all

  If Not (OpenWindowedScreen(0, 0, 0, 640, 480, 0, 0, 0))
     MessageRequester("Information", "Cannot initialize Screen!", 0)
    End
  EndIf
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:

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
Once again: thank you for being so kind to help me with this issue,

Posted: Wed Jul 04, 2007 6:24 pm
by Trond
If you use CompilerIf the unused code will get dropped from the exe (CompilerIf = #ifdef)

Posted: Wed Jul 04, 2007 6:26 pm
by lodger
Trond wrote:If you use CompilerIf the unused code will get dropped from the exe (CompilerIf = #ifdef)
Great!! I really begin to like PureBasic ... what a cool Language!! :)

Posted: Wed Jul 04, 2007 6:28 pm
by Kaeru Gaman
If (#PB_Compiler_OS = #PB_OS_Windows)
I would recomment to use CompilerIf...

in the end, you have to compile different executables for each OS,
so, a compiletime-if is the better choice... ;)

btw: better use codetags when posting..

PS: barn... Trond were too fast...