Need a #define for Visual Studio Versions that Include Secure String Functions (to Avoid _CRT_SECURE_NO_DEPRECATE)

前端 未结 2 1301
梦如初夏
梦如初夏 2021-01-25 07:13

A while back I tried to use Visual Studio 2010 to compile an MFC program that used a library I had written in Visual Studio 2003. Not surprisingly, I got a bunch of warnings abo

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 07:51

    I found a solution; the _MSC_VER macro/define makes this simple. Since the secure string functions were added in Visual Studio 2005 (VC++ version 1400, then it is sufficient to do something like this:

    #if _MSC_VER < 1400
        #define  _itoa_s(a,b,c)             _itoa(a,b,c)
        #define  wcscpy_s(a,b,c)            wcscpy(a,c)
        #define  _tprintf_s                 _tprintf
        #define  _sntprintf_s(a,b,c,d,...)  _sntprintf(a,c,d,...)
        …
    #endif
    

    Now when the code is compiled under VS2005+, it will have the added security, and when compiled on VS2003-, it will still compile without modification, albeit without the extra security.

    This makes porting and updating easier because you can update the library functions and use the secure string functions in the code even if you can’t compile them with VS2005+ just yet. This way when you do upgrade the compiler, you won’t have to make any changes to the library or the code to reap the benefits. It also makes it easier to work on the same code-base on older and newer versions of Visual Studio concurrently (at least to some degree).

提交回复
热议问题