Sign in to follow this  
Dom_152

Get Operating System at Run Time

Recommended Posts

Is it possible to get the Operating System the user is running an application on while the application is running? I know how to use defines to work out what platform it was built on and I can still use that method if need be. But it would be a lot more convinient if I could get it during run time.

Share this post


Link to post
Share on other sites
What language are you using? If you are using C/C++, I wouldn't really see the point because the executables are OS-specific anyway. Unless you want to check for version differences like Win98 vs WinXP.

Share this post


Link to post
Share on other sites
This is, ironically, OS-dependent. Windows has a function for returning the OS version, and (probably) so does OS X. In Linux, you might be able to determine the kernel version, perhaps even the distribution, but the OS is so flexible that there is no real notion of a "version" to be read.

Why, precisely, are you looking for this information?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you're talking about C++, I would just put platform dependent code within a try-except block.

Share this post


Link to post
Share on other sites
If you want the version of a specific OS, use the OS's functionality for that.
If you want the type of OS (MacOS, Windows), you know it at compile time, so it is impossible to change it at run time.

Quote:

If you're talking about C++, I would just put platform dependent code within a try-except block.


That's not a solution. It just won't compile.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
If you're talking about C++, I would just put platform dependent code within a try-except block.


That doesn't make any sense, given that try-except is a Windows specific construct.

Anyway, given that you must decide between Windows/Linux at compile time (since you cannot possibly build a binary which runs on both), I assume your question is actually about operating system versions. For Windows, the function to call to get the operating system version is GetVersionEx.

Share this post


Link to post
Share on other sites
Oops that was one of my stupid moments. Thanks for the replies. Could anyone remind me of what the defines are for the different operating systems?
Ha I just noticed your user rating bakery2k1: User Rating: 1337 :P
Hey it tickled me :P

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
I know how to use defines to work out what platform it was built on and I can still use that method if need be.

http://predef.sourceforge.net/
i find it funny that within 30 mins you have forgot the defines!

Share this post


Link to post
Share on other sites
Quote:

That doesn't make any sense, given that try-except is a Windows specific construct.


I figured he meant try/catch. try/except is niether C++ (which using try/catch) nor Windows SEH-specific (which uses __try/__except). So I guess it could either way, but either way, it won't work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
I know how to use defines to work out what platform it was built on and I can still use that method if need be.

http://predef.sourceforge.net/
i find it funny that within 30 mins you have forgot the defines!


He said he knows how to use defines to work this problem out. He didn't say he knew what the specific OS defines were...

Anyway,

__WIN32__ for windows
__APPLE_CC__ for mac
__LINUX__ for linux

those are the ones i know...


Share this post


Link to post
Share on other sites
the try/catch block can't work either ... because it is platform specific code. You can't CALL a function that your program doesn't know about, and when it is compiled for each platform it will only be linked to the libraries for that platform. So all fundamentally incompatible distinctions are made at compile time (using the defines). Once you know the top-level platform at compile time, you can invoke the platform specific run-time functions from their.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this