char* file = new char[strlen(lpszPathName)];
strcpy(file, lpszPathName);
错误提示:
error C2664: 'strlen' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *'
error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR' to 'const char *'
将strlen和strcpy换成 wcslen和 wcscpy之后第一个错误能解决,但是第二个不能解决。
strcpy(file, lpszPathName);
错误提示:
error C2664: 'strlen' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *'
error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR' to 'const char *'
将strlen和strcpy换成 wcslen和 wcscpy之后第一个错误能解决,但是第二个不能解决。
delete下就会崩掉。
_tcscpy替换掉strcpy
_tcslen替换掉strlen如果用比较新版的VC,最好是用微软C Runtime Library中提供的安全函数,一般于_s结尾的。
eg: strcpy_s...
char* file = new char[strlen(lpszPathName)];
strcpy(file, lpszPathName);
另外,LPCTSTR = const char* or const wchar_t* depending on _UNICODE
strlen和strcmp是在多字节的环境下的
stcrpy(file, (LPSTR)(LPCTSTR)lpszPathName);
_tcscpy(file, lpszPathName);因为strlen和strcpy只能作用于ASCII的字符集,而你当前的字符集是UNICODE.所以会报错
[Quote=引用 9 楼的回复:]
TCHAR* file = new TCHAR[_tcslen(lpszPathName) * sizeof(TCHAR)];
_tcscpy(file, lpszPathName);因为strlen和strcpy只能作用于ASCII的字符集,而你当前的字符集是UNICODE.所以会报错
[Quote]
The strcpy function copies strSource, including the terminating null character.