简体   繁体   中英

C++ MFC- strcpy error

I'm fairly new to C++ MFC programming. I'm having a hard time on understanding the code of C++. Just came from Java programming and it seems a whole new world to me. This is a long way from Android development.

Can you guys help me understand this problem here?

Here is the complete code

CString c;
code.GetWindowText(c);
AfxMessageBox(c);

OpenClipboard();
EmptyClipboard();

// Setup a memory HANDLE for the clipboard
HANDLE hMem = GlobalAlloc(GMEM_FIXED, c.GetLength() + 1);
char* pStr = (char*)GlobalLock(hMem); 
strcpy(pStr, c); //error here 
GlobalUnlock(hMem);

// Tell Clipboard to use our handle now.
::SetClipboardData(CF_TEXT, hMem);

CloseClipboard();

This part of the function copies the CString and assigns it to the clipboard. I'm getting the error

no suitable conversion function from CString to const char* exist.

I had an online reference and this is the code and other comments didn't seem to have the same problem. I'm wondering what the heck is wrong with mine.

The default build these days is Unicode, which uses 16-bit wchar_t characters that are incompatible with the char type.

CString comes in two specialized variants, CStingA for char characters and CStringW for wchar_t characters. If you use CStringA it will allow an automatic conversion to const char * .

A better way to go would be to use CF_UNICODETEXT and keep using the normal CString . You'll need to use wcscpy in place of strcpy , and allocate twice the number of bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM