2010-04-22 183 views
0

我得到一个“strcpy的”错误和以下行警告:“strcpy的”错误和警告

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

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

不知道为什么我得到一个“strcpy的”错误或警告,因为我不使用'strcpy'。与此相关的唯一线路是:

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 

输出是:

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 

上,这可能意味着什么?任何想法?

这些都是我的头:

iostream 
windows.h 
stdio.h 
tchar.h 
winnt.h 

回答

0

strcpy复制字符,直到它击中一个\0空终止符。这可能会导致缓冲区溢出和其他不良情况。 strcpy_s只复制指定数量的字符,因此您可以告诉它在超出缓冲区之前停止复制。详情请参阅http://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx

+1

'strcpy'被隐藏在宏的下面或所以在这里;正如OP所提到的,他们得到了一些不是'strcpy'的警告。 – Joey 2010-04-22 17:27:24

+0

我认为最好使用strncpy作为标准的C++而不是strcpy_s,这似乎是一个MS事物。 – 2010-04-22 18:37:17

1

LPCTSTR表示一个const TCHAR指针。 _tcscpy的第一个参数需要一个非常量TCHAR指针,即LPTSTR

尝试这样:

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

PS:即使这是最有可能不正确。给我们更多的上下文(更多周边代码),我们将能够更好地为您提供帮助。

+0

+1不适用于随机指针。其次,更多信息的请求 - 也许是'std :: string'会起作用。 – 2010-04-22 18:39:23

1

LPCTSTR是指向常量字符串的typedef名称。您不能将任何内容复制到常量字符串。

至于警告,这是Microsoft编译器自行发布的内容。如果您想使用strcpy,请禁用该警告。信息本身告诉你如何去做。

0

该警告告诉您strcpy已弃用(显然_tcscpy来电strcpy)。

_tcscpy的第一个参数是目标字符串,所以它不能是常量。 LPCTSTR中的'C'表示const

+0

我不这么认为。警告消息说: 错误C2664:'strcpy':无法将参数1从'LPCTSTR'转换为'char *' 转换失去限定符“ – 2010-04-22 17:32:29

+0

http://msdn.microsoft.com/en-us/library/kk6xf663( VS.80).aspx – 2010-04-22 18:10:13

+0

你是对的,之前没有看到C4996。 – 2010-04-23 08:16:09