I know there is a similarly titled question already on SO but I want to know my options for this specific case.
MSVC compiler gives a warning about strcpy:
1>c:\something\mycontrol.cpp(65): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Here's my code:
void MyControl::SetFontFace(const char *faceName) { LOGFONT lf; CFont *currentFont = GetFont(); currentFont->GetLogFont(&lf); strcpy(lf.lfFaceName, faceName); <--- offending line font_.DeleteObject(); // Create the font. font_.CreateFontIndirect(&lf); // Use the font to paint a control. SetFont(&font_); } Note font_ is an instance variable. LOGFONT is a windows structure where lfFaceName is defined as TCHAR lfFaceName[LF_FACESIZE].
What I'm wondering is can I do something like the following (and if not why not):
void MyControl::SetFontFace(const std::string& faceName) ... lf.lfFaceName = faceName.c_str(); ... Or if there is a different alternative altogether then let me know.
_CRT_SECURE_NO_WARNINGSpermanently in your settings and be done with it.strncpyis more portable, same safety.strncpyis that it is broken: if it hast to truncate it won't null terminate. So better to use a quick wrapper that always adds zero at the end.