此功能或变量可能不安全。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS

时间:2016-03-30 11:28:50

标签: c++

我正在研究C ++ DDL,但是我在某些地方遇到了以下问题:

C4996 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

我确实试过#define _CRT_SECURE_NO_WARNINGS,但问题仍然存在 这是代码:

sprintf(szDebugString, "%s: 0x%x (%s%s%i)", ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);

5 个答案:

答案 0 :(得分:8)

您必须在_CRT_SECURE_NO_WARNINGS之前定义#include <Windows.h>

或者,使用安全版本:

sprintf_s(szDebugString, sizeof(szDebugString), "%s: 0x%x (%s%s%i)",
    ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);

答案 1 :(得分:3)

将此定义放入stdafx.h

E.g。

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

答案 2 :(得分:1)

在我看来,在Windows项目中,禁用警告不是一个好主意。一个更好的主意是改进代码。忽略警告不仅会使潜在的代码漏洞未被注意到,而且在引入other potential code vulnerabilities时也会使程序员蒙蔽。

答案 3 :(得分:1)

要在Visual Studio IDE中关闭整个项目的警告:

1-打开项目的“属性页”对话框。

2-选择“配置属性”>“ C / C ++”>“高级”页面。

3-编辑“禁用特定警告”属性以添加4996。选择“确定”以应用更改。

答案 4 :(得分:0)

从文档中

  

您可以通过使用警告编译指示#pragma warning(suppress:4996)关闭特定代码行的警告。您也可以使用警告编译指示#pragma warning(disable:4996)关闭文件中的警告。

https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(C4996)%26rd%3Dtrue&view=vs-2017