Reputation: 1580
I'm working on a C++ DDL, however I get the following issue in some places:
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.
I did try #define _CRT_SECURE_NO_WARNINGS
, but the issue remains.
This is the code:
sprintf(szDebugString, "%s: 0x%x (%s%s%i)", ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);
Upvotes: 11
Views: 38490
Reputation: 465
To turn off the warning for an entire project in the Visual Studio IDE:
1- Open the Property Pages dialog for your project.
2- Select the Configuration Properties > C/C++ > Advanced page.
3- Edit the Disable Specific Warnings property to add 4996. Choose OK to apply your changes.
Upvotes: 5
Reputation: 5881
From the docs:
You can turn off the warning for a specific line of code by using the warning pragma, #pragma warning(suppress : 4996). You can also turn the warning off within a file by using the warning pragma, #pragma warning(disable : 4996).
Upvotes: 0
Reputation: 2684
In my point of view, on a Windows project, it is not a good idea to disable the warning; a better idea is to improve the code. Mute the warning not just keeps this potential code vulnerability unnoticed, but also blinds programmers when introducing other potential code vulnerabilities.
Upvotes: 3
Reputation: 27221
put this define into stdafx.h
.
E.g.
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
Upvotes: 6
Reputation: 122401
You have to define _CRT_SECURE_NO_WARNINGS
before #include <Windows.h>
.
Alternatively, use the safe version:
sprintf_s(szDebugString, sizeof(szDebugString), "%s: 0x%x (%s%s%i)",
ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);
Upvotes: 15