tags:

views:

174

answers:

5

I'm getting a 'strcpy' error and warning for the following lines:

_tcscpy(strCommandLine,_T("MyProgram.exe /param1"));

_tcscpy(strApplicationName,_T("MyProgram.exe"));

Not sure why I'm getting a 'strcpy' error or warning since I'm not using 'strcpy'. The only lines related to this is:

LPCTSTR strApplicationName;
LPTSTR strCommandLine;
_tcscpy(strCommandLine,_T("MyProgram.exe /param1"));   //warning is on this line     
_tcscpy(strApplicationName,_T("MyProgram.exe"));       //error is on this line

The output is:

1>c:\documents and settings\X.X\my documents\sandbox\sample.cpp(52) : 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.

1>        c:\program files\microsoft visual studio 8\vc\include\string.h(74) : see declaration of 'strcpy'

1>c:\documents and settings\X.X\my documents\sandbox\sample.cpp(53) : error C2664: 'strcpy' : cannot convert parameter 1 from 'LPCTSTR' to 'char *'

1>        Conversion loses qualifiers

Any ideas on what this could means?

These are my headers:

iostream
windows.h
stdio.h
tchar.h
winnt.h
A: 

strcpy copies characters until it hits a \0 null terminator. This can cause buffer overruns and other badness. strcpy_s copies only the specified number of characters, so you can tell it to stop copying before it overruns the buffer. See http://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx for details.

kevingessner
The `strcpy` is hidden beneath macros or so here; as the OP mentioned, they get the warning for something that's not `strcpy`.
Joey
And I think it would be better to use strncpy as that's standard C++ rather than strcpy_s which seems to be a MS thing.
Mark B
+1  A: 

LPCTSTR means a const TCHAR pointer. The first argument of _tcscpy expects a non-const TCHAR pointer i.e LPTSTR.

Try something like this:

TCHAR strApplicationName[2000];
TCHAR strCommandLine[2000[;
_tcscpy(strCommandLine,_T("MyProgram.exe /param1"));   //warning is on this line     
_tcscpy(strApplicationName,_T("MyProgram.exe")); 

PS: Even this is most probably incorrect. Give us more context (more surrounding code) and we will be able to help you better.

Vulcan Eager
+1 For not strcpy'ing into random pointers. Second the request for more information - maybe `std::string` would work.
Mark B
+1  A: 

LPCTSTR is a typedef name for a pointer to a constant string. You can't copy anything to a constant string.

As for the warnings, this is something Microsoft compiler puts out in its own accord. If you want to use strcpy, disable the warning. The message itself tells you how to do it.

AndreyT
A: 

The warning tells you that strcpy is deprecated (obviously _tcscpy calls strcpy).

The first parameter of _tcscpy is the destination string, so it can't be constant. The 'C' in LPCTSTR means const.

Bertrand Marron
I don't think so. The warning message says: error C2664: 'strcpy' : cannot convert parameter 1 from 'LPCTSTR' to 'char *' Conversion loses qualifiers"
Vulcan Eager
http://msdn.microsoft.com/en-us/library/kk6xf663(VS.80).aspx
Bertrand Marron
You're right. Didn't see the C4996 earlier.
Vulcan Eager
A: 

My guess is that the _tcscpy macro is using the strcpy function internally.

CodexDraco