Tell me please, why I get this issues:
if clipboard contains unicode chars (e.q. russian) I get only first selected word. First word before "space" character.
if clipboard not contains unicode chars (english only) I get first character of the selected text.
Get selected text:
CStringA getClipboard()
{
CStringA strData;
if (OpenClipboard(NULL)){
HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
char *pchData = (char*)GlobalLock(hClipboardData);
strData = pchData;
GlobalUnlock(hClipboardData);
CloseClipboard();
}
return strData;
}
Set text:
bool setClipboard(CStringA textToclipboard)
{
bool success = true;
if (OpenClipboard(NULL)){
EmptyClipboard();
HGLOBAL hClipboardData;
size_t size = (textToclipboard.GetLength()+1) * sizeof(TCHAR);
hClipboardData = GlobalAlloc(NULL, size);
TCHAR* pchData = (TCHAR*)GlobalLock(hClipboardData);
memcpy(pchData, LPCTSTR(textToclipboard.GetString()), size);
SetClipboardData(CF_UNICODETEXT, hClipboardData);
GlobalUnlock(hClipboardData);
CloseClipboard();
}
return success;
}
Simply get and set clipboard contents.
CStringA str = getClipboard();
setClipboard(str);
CF_UNICODETEXT
uses UTF-16. On Windows,wchar_t
data elements are used for UTF-16, but your code is usingchar
instead.CStringA
is not compatible with UTF-16. You are mismatching the data in both functions, that is why you do not get the results you are expecting.One solution is to use
CStringW
instead ofCStringA
:If you need to stick with
CStringA
, then either:use
CF_TEXT
instead ofCF_UNICODETEXT
and let the clipboard handle conversions between Ansi and Unicode for you:convert to/from UTF-16 manually when using
CF_UNICODETEXT
:Another solution is to use
CString
instead of eitherCStringA
orCStringW
, and then useCF_TEXT
orCF_UNICODETEXT
depending on whetherTCHAR
is Ansi or Unicode:Both of them are Unicode...
But in Unicode, more that one byte represents a character. For example maybe 2 bytes is used for a character. Therefore:
When it's Russian, the string is like
It reads until space.
When it's English, the string is like
It reads nothing. (If you read first, it's because of order of storing bytes, LE or BE)
Note: Maybe I'm not precision in choosing words (Unicode, UTF, ...)