2

Having some issues with strcpy...

Getting this error:

strcpy' : cannot convert parameter 2 from 'WCHAR *' to 'const char *

Here is the code...

char FunctionName[ 256 ]; 
UFunction *pUFunc                        = NULL;
strcpy( FunctionName, pUFunc->GetFullName() );

And also:

WCHAR* UObject::GetFullName ()
{
    if ( this->Class && this->Outer )
    {
        static WCHAR ObjectName[ 256 ];

        if (Outer == NULL)
        {
            wsprintf(ObjectName, L"");
        }
        else
        {
            if (Outer->Outer)
                wsprintf(ObjectName, L"%s %s.%s.%s", Class->Name.GetName(), Outer->Outer->Name.GetName(), Outer->Name.GetName(), Name.GetName());
            else if(Outer)
                wsprintf(ObjectName, L"%s %s.%s", Class->Name.GetName(), Outer->Name.GetName(), Name.GetName());
        }
        return ObjectName;
    }

    return L"(null)";
}
E3pO
  • 493
  • 1
  • 9
  • 21
  • 3
    The error message, in this case, is pretty much clear -- `strcpy` simply doesn't deal with wide C-style strings. Furthermore, it doesn't really make sense to try and store on in an ordinary C-style string. –  Dec 28 '11 at 01:40
  • Note that both `strcpy()` and `wcscpy()` are for copying strings, not for converting strings. You need a different (non-standard) function to convert between plain `char` and `WCHAR`. – Jonathan Leffler Dec 28 '11 at 01:41
  • 1
    Your code is very confusing. You are accessing a member of an object pointer you have explicitly set to NULL beforehand. – Niklas B. Dec 28 '11 at 01:41

2 Answers2

10

You need wcscpy for WCHAR items, not strcpy. But the real problem is that you are trying to convert a wide string to a narrow string. WideCharToMultiByte since you seem to be on Windows.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • I tried that... 'wcscpy' : cannot convert parameter 1 from 'char [256]' to 'wchar_t *' – E3pO Dec 28 '11 at 01:39
  • @E3pO: obviously both parameters need to be of type `wchar_t *`, so you have to convert your ASCII string to unicode first – Niklas B. Dec 28 '11 at 01:39
  • @Niklas his target is narrow, so he is really trying to convert the wide result of the function to the narrow string. – bmargulies Dec 28 '11 at 01:41
  • @bmargulies: Maybe it's the other way round and his target should be wide. We can only assume, but converting from unicode to ASCII is not possible in the general case. **EDIT:** So your guess was better :) – Niklas B. Dec 28 '11 at 01:43
  • This is great!!!! I changed my project from Unicode to Multi-Byte and it works!!!!. Thanks a ton. – BoBoDev Oct 27 '17 at 23:38
0

It's pretty obvious from the error: strcpy expects const char* as the second parameter and you are passing WCHAR*

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88