Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,
I Have a problem in type casting.
here is the code:
header file:
C++
struct userdata
{
    char strUserName[20];
    char strMsg[100];
};

another.cpp file:
C++
CString m_txtSend;
strcpy(uinf.strMsg, m_txtSend);

The error:
error C2664: 'strcpy' : cannot convert parameter 2 from 'CString' to 'const char *'

another Error:
cpp file:
C++
CString sUserName;
sUserName.Format("%s : %s", udata->strUserName, udata->strMsg);

the error:
error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [8]' to 'const wchar_t *'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC_DLL<wchar_t>
1>        ]
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

please help me.
Posted
Updated 22-Oct-19 22:12pm
v2

1. See CString Operations Relating to C-Style Strings[^].

2. You are trying to mix ASCII and Unicode types. Use CStringA with std::string, or CStringW with std::wstring, to avoid the confusion.
 
Share this answer
 
v2
Comments
Andreas Gieriet 9-Oct-13 3:18am    
My 5!
Cheers
Andi
Richard MacCutchan 9-Oct-13 3:31am    
Thanks. I'm still amazed at how many "developers" still don't understand the differences.
Albert Holguin 11-Oct-13 0:20am    
Probably the fault of schools not addressing the topic sufficiently (I would think). +5
Richard MacCutchan 11-Oct-13 2:42am    
Thanks. It's one of many unfortunately.
The other answers that discuss the difference between CStringA and CStringW are correct. You're dealing with an ASCII vs Unicode issue.

The Microsoft method to deal with this involves the use of a few macros.

TCHAR represents a character type and will automatically resolve to ASCII or Unicode depending on your project settings.

LPCSTR is a type defe to convert CStringA to const char *
LPCTSTR is a type defe to convert CString to TCHAR *
LPCWSTR is a type defe to convert CString to const wchar_t *

_tcscpy is an analog of strcpy, but will resolve to whichever tpye that TCHAR resolves to.

To do it the Microsoft recommended way:
C++
struct userdata
{
    TCHAR strUserName[20];
    TCHAR strMsg[100];
}
 
// another.cpp file:
CString m_txtSend;
_tcscpy(uinf.strMsg, m_txtSend);




C++
CString sUserName;
sUserName.Format("%s : %s", udata->strUserName, udata->strMsg);



If you must use char, then there is a way to make that work if you know that you're strings will always be ASCII.


C++
struct userdata
{
    char strUserName[20];
    char strMsg[100];
}

// another.cpp file:
CString m_txtSend;
strcpy(uinf.strMsg, (LPCSTR) (CStringA) m_txtSend);


When using format to convert between Unicode and ASCII or vice versa, use the uppercase %S.

C++
CString sUserName;
sUserName.Format("%S : %S", udata->strUserName, udata->strMsg);
 
Share this answer
 
Comments
Hanoi 2014 12-Oct-13 5:11am    
Thanks, it was very good help!
Hi,
You have to use the function GetBuffer from CString class like this:
C++
int len = m_txtSend.Length();
char* buffer = m_txtSend.getBuffer(len);
strcpy(uiinf.strMsg,buffer); // show to use strcpy_s function that is more secure
m_txtSend.ReleaseBuffer(); // not omit this call 


Best regards.
 
Share this answer
 
 
Share this answer
 
CString str; //str = some string assigned
const char* cChar = (const char*)str.GetBuffer(0);
 
Share this answer
 
Comments
Stefan_Lang 9-Aug-19 10:35am    
Necroing 6 year old problems doesn't help anyone
richie_rich_2 23-Oct-19 5:02am    
You would be wrong Mr Lang. These old posts are very helpful. Not sure what benefit your post added though...
Stefan_Lang 28-Oct-19 7:19am    
I beg to disagree. Most questions either are specific to a problem that is only of interest to the original poster - who is unlikely to be interested in an answer coming months or even years later. Otherwise it may be a question that gets asked over and over again in similar form here, at stackoverflow, or elsewhere - and would be easy to find with an appropriate internet search.

Not to mention that this answer is incorrect: the OP indicates that he is using wide characters, and the type cast suggested here wouldn't work.
Stefan_Lang 28-Oct-19 7:23am    
P.S.: also this question has an accepted answer (solution 4)! Unless that particular answer is proven to be wrong or incomplete, there is no reason to post another solution.
See this

char ch[20];
CString cs(L"Hai");
CStringA csA(cs);
strcpy(ch , csA.GetBuffer());
csA.ReleaseBuffer();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900