How to convert CString in MFC to char[] (character array)
5 Answers
I struggled with this, but what I use now is this: (UNICODE friendly)
CString strCommand("My Text to send to DLL.");
**
char strPass[256];
strcpy_s( strPass, CStringA(strCommand).GetString() );
**
// CStringA is a non-wide/unicode character version of CString This will then put your null terminated char array in strPass for you.
Also, if you control the DLL on the other side, specifying your parameters as:
const char* strParameter
rather than
char strParameter*
will "likely" convert CStrings for you with the default casting generally being effective.
1 Comment
Eliezer Miron
Thanks, this is the only solution that worked in my case.
You can use GetBuffer function to get the character buffer from CString.
1 Comment
dirkgently
The type is LPTSTR, strcpy will not work if UNICODE, _UNICODE is defined.