对于包含安全字符串函数的Visual Studio版本,需要一个#define(以避免_CRT_SECURE_NO_DEPRECATE) [英] Need a #define for Visual Studio Versions that Include Secure String Functions (to Avoid _CRT_SECURE_NO_DEPRECATE)

查看:185
本文介绍了对于包含安全字符串函数的Visual Studio版本,需要一个#define(以避免_CRT_SECURE_NO_DEPRECATE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我尝试使用Visual Studio 2010编译使用我在Visual Studio 2003中编写的库的MFC程序.毫不奇怪,我收到了一堆关于弃用和使用各种字符串函数的安全版本的警告.

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 about deprecation and using the secure versions of various string functions.

然后我更新了库中的相关功能以使用安全功能,并对其进行了很好的编译.

I then updated the relevant functions in the library to use the secure functions and it compiled fine.

后来,我尝试在其他使用Visual Studio 2003的系统上再次对其进行编译,并且对不存在的安全功能感到困惑.

I later tried to compile it again on the other system with Visual Studio 2003 and got nagged about the secure functions not existing.


我决定创建一种混合方法,使我可以在任何一种环境中编译使用该库的程序,并利用安全功能(如果可用),如果没有,则将其别名为旧功能.

I decided to create a hybrid approach that would allow me to compile programs that use the library in either environment, making use of the secure functions if available, and if not, aliasing them to the old ones.

起初,我考虑过检查每个功能,以查看是否存在安全版本,但这是行不通的,并且需要为每个功能单独进行工作:

At first I considered checking each function to see if a secure version exists, but that won’t work and requires separate work for each and every function:

#ifndef strcpy_s
    #define strcpy_s(a,b,c) strcpy(a,c)
#endif

#ifndef strcat_s
    #define strcat_s(a,b,c) strcat(a,c)
#endif

…

所以我想找出的是一种确定安全功能是否存在的方法.我知道它们是在Visual Studio 2005中引入的,但是是否有#define或可以按以下方式使用的内容?

So what I’m trying to figure out is a way to determine if the secure functions exist. I know that they were introduced in Visual Studio 2005, but is there a #define or something that can be used as follows?

#ifndef SECURE_FUNCTIONS  // or #ifdef VS_VER_2005, #if (VS_VER >= 0x2005) etc.
    #define strcpy_s(a,b,c) strcpy(a,c)
    #define strcat_s(a,b,c) strcat(a,c)
    …
#endif

我检查了crtdefs.h,但发现没有任何用处.

I checked crtdefs.h but found nothing useful.

推荐答案

我找到了解决方案; _MSC_VER 宏/define可以使此过程变得简单.由于安全的字符串函数

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

现在,当代码在VS2005 +下编译时,它将具有更高的安全性;而在VS2003-上编译时,即使没有额外的安全性,仍可以进行编译而无需修改.

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.

这使移植和更新更加容易,因为即使您还不能使用VS2005 +进行编译,也可以更新库函数并在代码中使用安全字符串函数.这样,当您升级编译器时,无需对库或代码进行任何更改即可获得好处.它还使在旧版本和更新版本的Visual Studio上同时在同一代码库上工作变得更加容易(至少在某种程度上).

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).

这篇关于对于包含安全字符串函数的Visual Studio版本,需要一个#define(以避免_CRT_SECURE_NO_DEPRECATE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆