Ignoreme
Ignoreme

Reputation: 183

Best Cross Platform way to obtain OS version in C++

Hey, I'm a bit new to C++ and am writing a simple program. My program will be using some folders in


Windows 7 path: C:\Users\%username%\Appdata\Local...

Windows XP path: C:\Documents and Settings\%username%\Local Settings\Application Data...

Unix: /home/%username%/.hiddenfolder/...


now the problem is windows. In my header file, I can do a nice

#ifdef _WIN32

to differentiate from windows and unix versions of the program, but during runtime I need to find if the user is useing XP or Vista/7 to set a correct path. Is there a standard way of doing this?

Upvotes: 4

Views: 821

Answers (4)

Kevin Zhang
Kevin Zhang

Reputation: 1

you can use WINVER to detect windows version

Upvotes: 0

Edward Strange
Edward Strange

Reputation: 40859

Those values are environment variables. You're looking at either %appdata% or $HOME/.app (not sure the MAC method, they may have "packages"). Since you're going to have to know what your target is at compile time (win vs. other), you can know which environment variable to look for. Then use getenv to fetch the value.

Upvotes: 0

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

You don't need OS version at all.

On *nixes (well, on Linux and OSX for sure, but should be on others too) you can use HOME environment variable. On Windows, you must (yes, must, because paths can be remapped/localised and hard-coding them is nice way to having more work than necessary) use SHGetFolderPath function (it's marked as deprecated, but it's not going anywhere any time soon, and newer SHGetKnownFolderPath is >=Vista), e.g.

TCHAR buffer[MAX_PATH];
HRESULT res = SHGetFolderPath(
    NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, buffer
);

if (SUCCEEDED(res)) {
    // ...
}

Upvotes: 8

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

Version detection is neither necessary nor sufficient, since these settings can be changed from their defaults. Use SHGetKnownFolderPath(FOLDERID_RoamingAppData, ...).

Upvotes: 1

Related Questions