Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

windows

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?

like image 842
Ignoreme Avatar asked Jan 21 '23 12:01

Ignoreme


1 Answers

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)) {
    // ...
}
like image 164
Cat Plus Plus Avatar answered Jan 30 '23 21:01

Cat Plus Plus