How to determine the OS in use?

Just starting out? Need help? Post your questions and find answers here.
lodger
User
User
Posts: 13
Joined: Mon May 28, 2007 6:08 pm
Location: Germany

How to determine the OS in use?

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
lodger
User
User
Posts: 13
Joined: Mon May 28, 2007 6:08 pm
Location: Germany

Post by lodger »

Thank you very much, that's just what I've been searching for ... :)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
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
lodger
User
User
Posts: 13
Joined: Mon May 28, 2007 6:08 pm
Location: Germany

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It's still my opinion that #PB_Compiler_OS is what you need.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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 ?
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
lodger
User
User
Posts: 13
Joined: Mon May 28, 2007 6:08 pm
Location: Germany

Post 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,
Last edited by lodger on Wed Jul 04, 2007 6:36 pm, edited 2 times in total.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

If you use CompilerIf the unused code will get dropped from the exe (CompilerIf = #ifdef)
lodger
User
User
Posts: 13
Joined: Mon May 28, 2007 6:08 pm
Location: Germany

Post 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!! :)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
Post Reply