Deprecated commands in Visual C++ - c++

When building a solution in Visual Studio 2013 of a project, I noticed that I am getting warnings for the following references:
warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\JCSocket.cpp 77 1
warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\JCSocket.cpp 82 1
warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\JCSocket.cpp 121 1
warning C4996: 'inet_ntoa': Use inet_ntop() or InetNtop() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\MuninNodeServer.cpp 64 1
warning C4996: 'GetVersionExW': was declared deprecated src\plugins\disk\DiskTimeMuninNodePlugin.cpp 48 1
warning C4996: 'GetVersion': was declared deprecated src\plugins\external\ConsolePipe.cpp 12 1
warning C4996: 'GetVersionExW': was declared deprecated src\plugins\PerfCounterMuninNodePlugin.cpp 56 1
warning C4996: 'GetVersionExW': was declared deprecated src\plugins\uptime\UptimeMuninNodePlugin.cpp 34 1
Whenever I try to change it to the recommended IntelliSense command, it is saying:
IntelliSense: identifier "inet_ntop" is undefined \src\core\MuninNodeServer.cpp 64 31

These errors are telling you what to do. Microsoft is nice like that.
gethostbyname -> getaddrinfo
inet_addr -> inet_pton
inet_ntoa -> inet_ntop
As far as GetVersionExW and GetVersion Microsoft recommends using the appropriate Version Helper Function.

Visual Studio tells you this warning cause you trying to use unsecure function which means includes the body of the function
and the body can obviously contain a signed overflow and there's a prospect to get another compiling errors or perhaps its not supported in the newer library
by the way, its about how you use this function
Simply you can disable the warning by add the following line
#pragma warning( disable : 4996)
at the top of your code
Which disable warning error code 4996 and will work fine without any problems.
Another way:
from the Solution Explorer right click on the project and choose Properties
navigate to Configuration Properties >> C/C++ >> Advanced
at the end just put 4996 in disable specific warning and click Apply >> Ok
Note: I prefer code way at all, It's more good to learn if you're not
programming in visual studio or making a header file for
your library

Related

In Visual Studio how do I avoid spurious Intellisense warning

I have the latest build of VS 2019 on Windows 10. I am writing two DLLs in standard C++ (not .Net).
DLL #1 calls a function in DLL #2. I included the .lib file of DLL #2 in DLL #1.
The code compiles fine and the function call works perfectly at runtime. My issue is that green squiggles appear under the function name with the message: Function definition for 'RunJaguar' not found.
DWORD RunJaguar(EXTENSION_CONTROL_BLOCK* pECB);
My psychic powers suggest you should declare your DLL functions with "extern" to make Intellisense happy:
extern DWORD RunJaguar(EXTENSION_CONTROL_BLOCK* pECB);
Or more appropriately, use __declspec(dllimport) and __declspec(dllexport) for DLL exports and imports.
You could use warning pragma to avoid spurious Intellisense warning.
#pragma warning( push,n )//Specify the compilation warning level as Level n
//Your code
//...
#pragma warning( pop )//Restore the previous compilation warning level
Here is the level:
Warning
Level Meaning
-------- -------------------------------------------
0 Turns off emission of all warning messages.
1 Displays severe warning messages
2 Displays level 1 warnings plus certain, less-severe warnings, such
as warnings about hiding class members
3 Displays level 2 warnings plus certain, less-severe warnings, such
as warnings about expressions that always evaluate to true or false
4 (the default) Displays all level 3 warnings plus informational warnings

How can I enable compiler warnings in Visual Studio 2019?

The reason that I ask this question is this link below:
Why can this function return a C++ int reference?
It seems that the compiler is bad at reporting mistakes such as: return a value from a function.
So I want to activate them in Visual Studio 2019, but it did not work after I set it (restart IDE) like below:
I suggest you could try to use the following method to enable warnings that are off by default:
1,#pragma warning(default : warning_number )
The specified warning (warning_number) is enabled at its default level. Documentation for the warning contains the default level of the warning.
2,#pragma warning( warning_level : warning_number )
The specified warning (warning_number) is enabled at the specified level (warning_level).
3,/Wall
/Wall enables all warnings that are off by default. If you use this option, you can turn off individual warnings by using the /wd option.
4,/wLnnnn
This option enables warning nnnn at level L.
For more details anbout warning level, I suggest you could refer to the link:https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=vs-2019

How to enable warnings through pragma?

I'm trying to enable some warnings that detail potential security problems through a pragma. The warnings are those listed by Jon Sturgeon in “Off By Default” Compiler Warnings in Visual C++:
# pragma warning (enable: 4191 4242 4263 4264 4265 4266 4302 4826 4905 4906 4928)
I'm getting a warning under Visual Studio (2010 to be exact) for the above pragma:
ClCompile:
pch.cpp
... config.h(238): warning C4615: #pragma warning : unknown user warning type
It appears there is no enable or on for pragma warning. There is a once, but I want to see all instances of the potential problems, and not just one.
Microsoft's documentation on warning does not appear to discuss the topic of enabling warnings.
I can't really use # pragma warning (default: ...) because they are off by default. I don't want to modify each of the solution's configurations because the solution has 4 projects, 6 configurations and 4 platforms. So I have 80+ of them to change (the cross product of the selections).
How do I enable select warnings through the pragma?

How to disable warning C4927 in VS2013 Community

I'm receiving the following warning in my project:
warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while calling the constructor 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(const _Elem *)'
with
[
_Elem=char
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xstring(778) : see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string'
I understand why it's happening, I'm just unable to suppress it. I've tried adding it to the Disable Specific Warnings list in my project settings, and I've also set my warning level to Turn Off All Warnings (/W0), yet the warning persists. Does anyone have any recommendations on how to hide the message?
You can control Visual Studio warnings directly in code using #pragma warning (https://msdn.microsoft.com/en-us/library/2c8f766e.aspx). If you'd like to silence the warning for a single line, you can put the following immediately before the line:
#pragma warning (suppress: 4927)
line-that-causes-warning-4927
You can also disable it from any point past the #pragma, by using 'disable' instead of 'suppress'. However, as the comments suggest, it's better to actually fix the warning, because it could be causing issues in your program.

Error preprocessor directives when building

When building a VS2013 solution (migrated from VS8) I get the following error :
Error 1 error C2220: warning treated as error - no 'object' file
generated C:\Program Files\Microsoft Visual Studio
12.0\VC\atlmfc\include\afx.h 38 Warning 2 warning C4996: 'MBCS_Support_Deprecated_In_MFC': MBCS support in MFC is deprecated
and may be removed in a future version of MFC. C:\Program
Files\Microsoft Visual Studio 12.0\VC\atlmfc\include\afx.h 38
This is caused bij the following code :
#ifdef _MBCS
// Warn about MBCS support being deprecated: see http://go.microsoft.com/fwlink/p/?LinkId=279048 for more information.
#pragma warning(push)
#pragma warning(1 : 4996)
inline __declspec(deprecated("MBCS support in MFC is deprecated and may be removed in a future version of MFC.")) void MBCS_Support_Deprecated_In_MFC() { }
class MBCS_Deprecated_MFC
{
public:
MBCS_Deprecated_MFC() { MBCS_Support_Deprecated_In_MFC(); }
};
#pragma warning(pop)
#endif
How can I find where _MBCS is defined in the solution. Find doesn't has any results.
The _MBCS symbol will be defined as a result of the settings in your project properties. Look at General->Character Set - this is what adds the required entries to the command line.
To continue using MBCS, you need to install the optional support from Microsoft here
As it notes in MSDN:
The code in your question actually gives a link to this blog post, which discusses the changes and includes a link to the download:
// Warn about MBCS support being deprecated: see http://go.microsoft.com/fwlink/p/?LinkId=279048 for more information.
So, you can either download the patch from the link above or migrate your application to UNICODE.