Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ compile time macros to detect windows os

Are there any C++ compile time macros which exists to detect which Windows OS the code is being compiled on. I basically want to support certain functions only on Win7. So I am interested in doing something like this

#if <os_macro> = WIN7
  // This function would do something valid only on Win7 builds.
  bool myfunction {
        // do something here
  }
#else
  // This function would typically return false, since its not supported on OS below win7
  bool myfunction {
       return false;
  }
#endif 

Is there any other better way to do this?

like image 742
Rajiv Avatar asked Apr 11 '12 18:04

Rajiv


1 Answers

The OS that it's getting compiled on is not all that important; what matters more is the OS that the code is running on, which you obviously cannot detect at compile time. But if you want your code to run on older versions of Windows, you can set WINVER and _WIN32_WINNT to certain values, which will cause newer functions not to be available etc. (just search the Windows header files for where those macros get tested to get an idea).

To test for functionality at runtime, use GetProcAddress (and possibly also LoadLibrary, if it's in a newer DLL) to test if the function is available. If it is, call it, if not, don't.

See also the predefined macros used by the Visual Studio compiler if you want to detect the compiler version etc.

like image 115
Adam Rosenfield Avatar answered Sep 30 '22 01:09

Adam Rosenfield