Convert CString to character array?

How to convert CString to MFC to char [] (character array)

+5
source share
4 answers

You use CString :: GetBuffer () to get the TCHAR [] pointer to the buffer. If you compiled without UNICODE, it determined that it is enough - TCHAR is the same as char, otherwise you will have to allocate a separate buffer and use WideCharToMultiByte () for conversion.

+7
source

I struggled with this, but now I use this: (UNICODE friendly)

CString strCommand ("My text to send to the DLL.");

**

char strPass[256];
strcpy_s( strPass, CStringA(strCommand).GetString() );

**

//CStringA - / CString char strPass .

, DLL , :

const char * strParameter

char strParameter *

"" CStrings , .

+4

You can use GetBuffer to get the character buffer from CString.

+3
source

Calling the GetBuffer method alone is not enough; you will also need to copy this buffer to an array.

For example:

CString sPath(_T("C:\temp\"));
TCHAR   tcPath[MAX_PATH]; 
_tcscpy(szDisplayName, sPath.GetBuffer(MAX_PATH));
+1
source

All Articles