views:

340

answers:

3

error C2664: 'strcpy' : cannot convert parameter 1 from 'TCHAR *' to 'char *' code:

LPCTSTR name, DWORD value
strcpy (&this->valueName[0], name);

error C2664: 'strlen' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *'

LPCTSTR name; 
strlen (name)     

The code above to a class which works fine in another project, I can't find the reason why it doesn't work in this MS VS2010 Project.

+4  A: 

Probably because TCHAR is defined to be a char in one of your projects, but not in the VS2010 one where it is probably wchar_t.

If your project defines UNICODE/_UNICODE, which is the same as specifying it to be a Unicode build in the project settings, TCHARs will be wchar_t.

You basically need to decide whether to use Unicode or not and if you do, you need to change the regular calls to strncpy et al to the wide-char equivalents or use the t-variants that change the same way as TCHARs do. Look at the help for strncpy or the other functions to see what the wide or t-variants are called.

You can also look at MSDN for the calls such as strcpy, where you can see that the wide-char version is called wcscpy and the t version is called _tcscpy. I would recommend you to stick with the t-versions if you are going to use the code in different projects that either use UNICODE or not, or to make an informed decision which one you are going to use and then stick with that. Which is better depends on your scenario I would say and may invoke some "religious" opinions...

villintehaspam
if so, how could I fix it?
Christoferw
@Christoferw: I updated my answer to give you a pointer on how to resolve this. Write another comment if you need more help.
villintehaspam
But i did not have a unicode project....
Christoferw
+2  A: 

Is your project a unicode project? If so I believe TCHAR will be equivalent to a wchar_t rather than a char making your conversion attempts invalid. See here for more info.

jkp
No, it's a Multibyte project
Christoferw
Well its obviously not or you won't be getting this problem...
Goz
It is selected at the project settings in Visual Studio:Character Set: Use Multi-Byte Character Set
Christoferw
+3  A: 

You need to use a function such as wcstombs when _UNICODE is defined. Either that or just use _tcslen (Look under Generic-Text Routine Mappings) on the TCHAR string and the compiler will transfer it to either strlen or wcslen depending if you are using unicode or not.

Goz
That post just saved me a couple hours. The default for new C++ projects is to use the unicode character set. Turning that off, fixed these errors.
Scott P