C windows version macro
I am working on porting some code from windows XP to Windows 7.
As per the article there are many API’s that are banned in windows 7 as in following link.
Now I want to use the same code for both XP and Windows 7. But the new API’s are not backward compatible. So I am looking for any simple macro’s that are available to determine which windows version I am compiling for and will use the API accordingly.
Please reply if anybody has any idea regarding this.
You have to check WINVER and/or WIN32_WINNT macros. Usually _WIN32_WINNT equals to WINVER
No, a function like strcpy() is absolutely NOT banned if you are using windows 7 SDK. Microsoft recommends using their crappy *_s() equivalents which are NOT portable and nobody uses except Visual Studio users.
There are no win32 APIs which cannot be used in windows 7 once the same code compiles fine with windows XP SDK.
There is _CRT_SECURE_NO_WARNINGS macro if you want to disable crappy VS warnings when you try to use «non-secure» functions, which btw are standard C functions.
Why the «n» Functions Are Banned The classic C runtime, «n» functions (such as strncpy and strncat) are banned because they are so hard to call correctly. |
I am completely speechless right now. what retard would allow this to be printed on MSDN. If this is the mind set that the current MS Development team had going into Windows 7 then I’m not going to wait around for Windows 8. I will keep my fond memories of this once great API and get out before they are all replaced with rage.
Setting aside whether the _s functions are evil, I think you are worrying about the Windows version too much. strcpy_s, etc are CRT functions, not Win32 API calls, so their availability depends on the CRT you are linking against, not the version of Windows you run on.
The macros proposed above should really be switching on the version of Visual C++, not Windows. The secure functions turned up with Visual Studio 2005.
The ban referred to in the article mentioned above must refer to Microsoft’s coding standards. Windows 7 runs our legacy code — with loads of old CRT calls — without complaint!
PS If you do use (eg) strcpy_s, note that the extra parameter is the buffer size, not the string length. And it’s the second, not the third parameter. (And dest comes before src in the param list!)
numberOfElements is the size the destination string buffer.
so something like this would work for arrays but NOT char*/wchar_t* pointers to new-ed buffer.
(wchar_t and TCHAR versions left as exercise for reader)
To handle dynamically allocated buffer, you will need a macro which allows you to provide the buffer size (and just ignores it in the non-_s case).
It might be better to provide a MY_STRCPY_S and then map it to strcpy when strcpy_s is not available.
Predefined macros
The Microsoft C/C++ compiler (MSVC) predefines certain preprocessor macros, depending on the language (C or C++), the compilation target, and the chosen compiler options.
MSVC supports the predefined preprocessor macros required by the ANSI/ISO C99, C11, and C17 standards, and the ISO C++14 and C++17 standards. The implementation also supports several more Microsoft-specific preprocessor macros. Some macros are defined only for specific build environments or compiler options. Except where noted, the macros are defined throughout a translation unit as if they were specified as /D compiler option arguments. When defined, the macros are expanded to the specified values by the preprocessor before compilation. The predefined macros take no arguments and can’t be redefined.
Standard predefined identifier
The compiler supports this predefined identifier specified by ISO C99 and ISO C++11.
__func__ The unqualified and unadorned name of the enclosing function as a function-local static const array of char .
Standard predefined macros
The compiler supports these predefined macros specified by the ISO C99, C11, C17, and ISO C++17 standards.
__cplusplus Defined as an integer literal value when the translation unit is compiled as C++. Otherwise, undefined.
__DATE__ The compilation date of the current source file. The date is a constant length string literal of the form Mmm dd yyyy. The month name Mmm is the same as the abbreviated month name generated by the C Runtime Library (CRT) asctime function. The first character of date dd is a space if the value is less than 10. This macro is always defined.
__FILE__ The name of the current source file. __FILE__ expands to a character string literal. To ensure that the full path to the file is displayed, use /FC (Full Path of Source Code File in Diagnostics). This macro is always defined.
__LINE__ Defined as the integer line number in the current source file. The value of the __LINE__ macro can be changed by using a #line directive. This macro is always defined.
__STDC__ Defined as 1 only when compiled as C and if the /Za compiler option is specified. Otherwise, undefined.
__STDC_HOSTED__ Defined as 1 if the implementation is a hosted implementation, one that supports the entire required standard library. Otherwise, defined as 0.
__STDC_NO_ATOMICS__ Defined as 1 if the implementation doesn’t support optional standard atomics. The MSVC implementation defines it as 1 when compiled as C and one of the /std C11 or C17 options is specified.
__STDC_NO_COMPLEX__ Defined as 1 if the implementation doesn’t support optional standard complex numbers. The MSVC implementation defines it as 1 when compiled as C and one of the /std C11 or C17 options is specified.
__STDC_NO_THREADS__ Defined as 1 if the implementation doesn’t support optional standard threads. The MSVC implementation defines it as 1 when compiled as C and one of the /std C11 or C17 options is specified.
__STDC_NO_VLA__ Defined as 1 if the implementation doesn’t support standard variable length arrays. The MSVC implementation defines it as 1 when compiled as C and one of the /std C11 or C17 options is specified.
__STDC_VERSION__ Defined when compiled as C and one of the /std C11 or C17 options is specified. It expands to 201112L for /std:c11 , and 201710L for /std:c17 .
__STDCPP_THREADS__ Defined as 1 if and only if a program can have more than one thread of execution, and compiled as C++. Otherwise, undefined.
__TIME__ The time of translation of the preprocessed translation unit. The time is a character string literal of the form hh:mm:ss, the same as the time returned by the CRT asctime function. This macro is always defined.
Microsoft-specific predefined macros
MSVC supports these additional predefined macros.
__ATOM__ Defined as 1 when the /favor:ATOM compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX__ Defined as 1 when the /arch:AVX , /arch:AVX2 , or /arch:AVX512 compiler options are set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX2__ Defined as 1 when the /arch:AVX2 or /arch:AVX512 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX512BW__ Defined as 1 when the /arch:AVX512 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX512CD__ Defined as 1 when the /arch:AVX512 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX512DQ__ Defined as 1 when the /arch:AVX512 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX512F__ Defined as 1 when the /arch:AVX512 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
__AVX512VL__ Defined as 1 when the /arch:AVX512 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.
_CHAR_UNSIGNED Defined as 1 if the default char type is unsigned. This value is defined when the /J (Default char type is unsigned) compiler option is set. Otherwise, undefined.
__CLR_VER Defined as an integer literal that represents the version of the Common Language Runtime (CLR) used to compile the app. The value is encoded in the form Mmmbbbbb , where M is the major version of the runtime, mm is the minor version of the runtime, and bbbbb is the build number. __CLR_VER is defined if the /clr compiler option is set. Otherwise, undefined.
_CONTROL_FLOW_GUARD Defined as 1 when the /guard:cf (Enable Control Flow Guard) compiler option is set. Otherwise, undefined.
__COUNTER__ Expands to an integer literal that starts at 0. The value is incremented by 1 every time it’s used in a source file, or in included headers of the source file. __COUNTER__ remembers its state when you use precompiled headers. This macro is always defined.
This example uses __COUNTER__ to assign unique identifiers to three different objects of the same type. The exampleClass constructor takes an integer as a parameter. In main , the application declares three objects of type exampleClass , using __COUNTER__ as the unique identifier parameter:
__cplusplus_cli Defined as the integer literal value 200406 when compiled as C++ and a /clr compiler option is set. Otherwise, undefined. When defined, __cplusplus_cli is in effect throughout the translation unit.
__cplusplus_winrt Defined as the integer literal value 201009 when compiled as C++ and the /ZW (Windows Runtime Compilation) compiler option is set. Otherwise, undefined.
_CPPRTTI Defined as 1 if the /GR (Enable Run-Time Type Information) compiler option is set. Otherwise, undefined.
_DEBUG Defined as 1 when the /LDd , /MDd , or /MTd compiler option is set. Otherwise, undefined.
_DLL Defined as 1 when the /MD or /MDd (Multithreaded DLL) compiler option is set. Otherwise, undefined.
__FUNCDNAME__ Defined as a string literal that contains the decorated name of the enclosing function. The macro is defined only within a function. The __FUNCDNAME__ macro isn’t expanded if you use the /EP or /P compiler option.
This example uses the __FUNCDNAME__ , __FUNCSIG__ , and __FUNCTION__ macros to display function information.
__FUNCSIG__ Defined as a string literal that contains the signature of the enclosing function. The macro is defined only within a function. The __FUNCSIG__ macro isn’t expanded if you use the /EP or /P compiler option. When compiled for a 64-bit target, the calling convention is __cdecl by default. For an example of usage, see the __FUNCDNAME__ macro.
__FUNCTION__ Defined as a string literal that contains the undecorated name of the enclosing function. The macro is defined only within a function. The __FUNCTION__ macro isn’t expanded if you use the /EP or /P compiler option. For an example of usage, see the __FUNCDNAME__ macro.
_INTEGRAL_MAX_BITS Defined as the integer literal value 64, the maximum size (in bits) for a non-vector integral type. This macro is always defined.
__INTELLISENSE__ Defined as 1 during an IntelliSense compiler pass in the Visual Studio IDE. Otherwise, undefined. You can use this macro to guard code the IntelliSense compiler doesn’t understand, or use it to toggle between the build and IntelliSense compiler. For more information, see Troubleshooting Tips for IntelliSense Slowness.
_ISO_VOLATILE Defined as 1 if the /volatile:iso compiler option is set. Otherwise, undefined.
_KERNEL_MODE Defined as 1 if the /kernel (Create Kernel Mode Binary) compiler option is set. Otherwise, undefined.
_M_AMD64 Defined as the integer literal value 100 for compilations that target x64 processors. Otherwise, undefined.
_M_ARM Defined as the integer literal value 7 for compilations that target ARM processors. Otherwise, undefined.
_M_ARM_ARMV7VE Defined as 1 when the /arch:ARMv7VE compiler option is set for compilations that target ARM processors. Otherwise, undefined.
_M_ARM_FP Defined as an integer literal value that indicates which /arch compiler option was set for ARM processor targets. Otherwise, undefined.
A value in the range 30-39 if no /arch ARM option was specified, indicating the default architecture for ARM was set ( VFPv3 ).
A value in the range 40-49 if /arch:VFPv4 was set.
_M_ARM64 Defined as 1 for compilations that target 64-bit ARM processors. Otherwise, undefined.
_M_CEE Defined as 001 if any /clr (Common Language Runtime Compilation) compiler option is set. Otherwise, undefined.
_M_CEE_PURE Deprecated beginning in Visual Studio 2015. Defined as 001 if the /clr:pure compiler option is set. Otherwise, undefined.
_M_CEE_SAFE Deprecated beginning in Visual Studio 2015. Defined as 001 if the /clr:safe compiler option is set. Otherwise, undefined.
_M_FP_EXCEPT Defined as 1 if the /fp:except or /fp:strict compiler option is set. Otherwise, undefined.
_M_FP_FAST Defined as 1 if the /fp:fast compiler option is set. Otherwise, undefined.
_M_FP_PRECISE Defined as 1 if the /fp:precise compiler option is set. Otherwise, undefined.
_M_FP_STRICT Defined as 1 if the /fp:strict compiler option is set. Otherwise, undefined.
_M_IX86 Defined as the integer literal value 600 for compilations that target x86 processors. This macro isn’t defined for x64 or ARM compilation targets.
_M_IX86_FP Defined as an integer literal value that indicates the /arch compiler option that was set, or the default. This macro is always defined when the compilation target is an x86 processor. Otherwise, undefined. When defined, the value is:
0 if the /arch:IA32 compiler option was set.
1 if the /arch:SSE compiler option was set.
2 if the /arch:SSE2 , /arch:AVX , /arch:AVX2 , or /arch:AVX512 compiler option was set. This value is the default if an /arch compiler option wasn’t specified. When /arch:AVX is specified, the macro __AVX__ is also defined. When /arch:AVX2 is specified, both __AVX__ and __AVX2__ are also defined. When /arch:AVX512 is specified, __AVX__ , __AVX2__ , __AVX512BW__ , __AVX512CD__ , __AVX512DQ__ , __AVX512F__ , and __AVX512VL__ are also defined.
_M_X64 Defined as the integer literal value 100 for compilations that target x64 processors. Otherwise, undefined.
_MANAGED Defined as 1 when the /clr compiler option is set. Otherwise, undefined.
_MSC_BUILD Defined as an integer literal that contains the revision number element of the compiler’s version number. The revision number is the fourth element of the period-delimited version number. For example, if the version number of the Microsoft C/C++ compiler is 15.00.20706.01, the _MSC_BUILD macro evaluates to 1. This macro is always defined.
_MSC_EXTENSIONS Defined as 1 if the on-by-default /Ze (Enable Language Extensions) compiler option is set. Otherwise, undefined.
_MSC_FULL_VER Defined as an integer literal that encodes the major, minor, and build number elements of the compiler’s version number. The major number is the first element of the period-delimited version number, the minor number is the second element, and the build number is the third element. For example, if the version number of the Microsoft C/C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706. Enter cl /? at the command line to view the compiler’s version number. This macro is always defined.
_MSC_VER Defined as an integer literal that encodes the major and minor number elements of the compiler’s version number. The major number is the first element of the period-delimited version number and the minor number is the second element. For example, if the version number of the Microsoft C/C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Enter cl /? at the command line to view the compiler’s version number. This macro is always defined.
Visual Studio version | _MSC_VER |
---|---|
Visual Studio 6.0 | 1200 |
Visual Studio .NET 2002 (7.0) | 1300 |
Visual Studio .NET 2003 (7.1) | 1310 |
Visual Studio 2005 (8.0) | 1400 |
Visual Studio 2008 (9.0) | 1500 |
Visual Studio 2010 (10.0) | 1600 |
Visual Studio 2012 (11.0) | 1700 |
Visual Studio 2013 (12.0) | 1800 |
Visual Studio 2015 (14.0) | 1900 |
Visual Studio 2017 RTW (15.0) | 1910 |
Visual Studio 2017 version 15.3 | 1911 |
Visual Studio 2017 version 15.5 | 1912 |
Visual Studio 2017 version 15.6 | 1913 |
Visual Studio 2017 version 15.7 | 1914 |
Visual Studio 2017 version 15.8 | 1915 |
Visual Studio 2017 version 15.9 | 1916 |
Visual Studio 2019 RTW (16.0) | 1920 |
Visual Studio 2019 version 16.1 | 1921 |
Visual Studio 2019 version 16.2 | 1922 |
Visual Studio 2019 version 16.3 | 1923 |
Visual Studio 2019 version 16.4 | 1924 |
Visual Studio 2019 version 16.5 | 1925 |
Visual Studio 2019 version 16.6 | 1926 |
Visual Studio 2019 version 16.7 | 1927 |
Visual Studio 2019 version 16.8, 16.9 | 1928 |
Visual Studio 2019 version 16.10 preview | 1929 |
To test for compiler releases or updates in a given version of Visual Studio or after, use the >= operator. You can use it in a conditional directive to compare _MSC_VER against that known version. If you have several mutually exclusive versions to compare, order your comparisons in descending order of version number. For example, this code checks for compilers released in Visual Studio 2017 and later. Next, it checks for compilers released in or after Visual Studio 2015. Then it checks for all compilers released before Visual Studio 2015:
To test for compiler versions that share major and minor numbers, use the major, minor, and build numbers in _MSC_FULL_VER for comparisons. The compilers in Visual Studio 2019 version 16.9 have an _MSC_FULL_VER value of 192829500 or greater.
For more information, see Visual C++ Compiler Version in the Microsoft C++ Team Blog.
_MSVC_LANG Defined as an integer literal that specifies the C++ language standard targeted by the compiler. It’s set only in code compiled as C++. The macro is the integer literal value 201402L by default, or when the /std:c++14 compiler option is specified. The macro is set to 201703L if the /std:c++17 compiler option is specified. It’s set to a higher, unspecified value when the /std:c++latest option is specified. Otherwise, the macro is undefined. The _MSVC_LANG macro and /std (Specify Language Standard Version) compiler options are available beginning in Visual Studio 2015 Update 3.
__MSVC_RUNTIME_CHECKS Defined as 1 when one of the /RTC compiler options is set. Otherwise, undefined.
- Available beginning with Visual Studio 2017 version 15.8: Defined as 0 when the preprocessor conformance mode /experimental:preprocessor compiler option is set. Defined as 1 by default, or when the /experimental:preprocessor- compiler option is set, to indicate the traditional preprocessor is in use.
- Available beginning with Visual Studio 2019 version 16.5: Defined as 0 when the preprocessor conformance mode /Zc:preprocessor compiler option is set. Defined as 1 by default, or when the /Zc:preprocessor- compiler option is set, to indicate the traditional preprocessor is in use (essentially, /Zc:preprocessor replaces the deprecated /experimental:preprocessor ).
_NATIVE_WCHAR_T_DEFINED Defined as 1 when the /Zc:wchar_t compiler option is set. Otherwise, undefined.
_OPENMP Defined as integer literal 200203, if the /openmp (Enable OpenMP 2.0 Support) compiler option is set. This value represents the date of the OpenMP specification implemented by MSVC. Otherwise, undefined.
_PREFAST_ Defined as 1 when the /analyze compiler option is set. Otherwise, undefined.
__TIMESTAMP__ Defined as a string literal that contains the date and time of the last modification of the current source file, in the abbreviated, constant length form returned by the CRT asctime function, for example, Fri 19 Aug 13:32:58 2016 . This macro is always defined.
_VC_NODEFAULTLIB Defined as 1 when the /Zl (Omit Default Library Name) compiler option is set. Otherwise, undefined.
_WCHAR_T_DEFINED Defined as 1 when the default /Zc:wchar_t compiler option is set. The _WCHAR_T_DEFINED macro is defined but has no value if the /Zc:wchar_t- compiler option is set, and wchar_t is defined in a system header file included in your project. Otherwise, undefined.
_WIN32 Defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM, x86, or x64. Otherwise, undefined.
_WIN64 Defined as 1 when the compilation target is 64-bit ARM or x64. Otherwise, undefined.
_WINRT_DLL Defined as 1 when compiled as C++ and both /ZW (Windows Runtime Compilation) and /LD or /LDd compiler options are set. Otherwise, undefined.
No preprocessor macros that identify the ATL or MFC library version are predefined by the compiler. ATL and MFC library headers define these version macros internally. They’re undefined in preprocessor directives made before the required header is included.