Best way to cross platform to get C ++ OS version

Hey, I'm a little new to C ++ and writing a simple program. My program will use several folders in


Path to Windows 7: C: \ Users \% username% \ Appdata \ Local ...

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

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


now the problem is windows. In my header file I can make nice

#ifdef _WIN32

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

+4
source share
4 answers

You don't need an OS version at all.

On * nixes (well, on Linux and OSX for sure, but it should be on others), you can use the HOME environment variable. On Windows, you must (yes, it is necessary, because paths can be reassigned / localized and hard-coded, this is a good way to get more work than necessary) use SHGetFolderPath (it was marked as deprecated, but it is not going anywhere in the near future, but new SHGetKnownFolderPath β†’ = Vista) e.g.

 TCHAR buffer[MAX_PATH]; HRESULT res = SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, buffer ); if (SUCCEEDED(res)) { // ... } 
+8
source

Version detection is neither necessary nor sufficient, as these parameters can be changed from their default values. Use SHGetKnownFolderPath(FOLDERID_RoamingAppData, ...) .

+1
source

These values ​​are environment variables. You look at either% appdata% or $ HOME / .app (not sure about the MAC method, they may have β€œpackets”). Since you need to know that your target is at compile time (win vs. other), you can find out which environment variable to look for. Then use getenv to get the value.

0
source

you can use WINVER to determine the version of Windows

0
source

Source: https://habr.com/ru/post/1339601/


All Articles