0

Copying to clipboard in HTML format works when I use char, but if I use wchar_t it doesn't work

When I paste it it's just EMPTY

here is my code Plase Help me

Or is there a better way to use unicode(not using wchar_t)? If you do help me

void copyStringEnd(wchar_t *string, wchar_t *buffer)
{
    int i = 0;
    int string_StartIndex = 0;

    while (string[string_StartIndex] != NULL)
    {
        string_StartIndex++;
    }

    while (buffer[i] != NULL)
    {
        string[string_StartIndex + i] = buffer[i];
        i++;
    }
    string[string_StartIndex + i] = '\0';
}

int main()
{
    wchar_t *html = L"abc";
    wchar_t *buf = (wchar_t*)malloc(400 + wcslen(html));

    wcscpy_s(buf, 200,
            L"Version:0.9\r\n"
            L"StartHTML:00000000\r\n"
            L"EndHTML:00000000\r\n"
            L"StartFragment:00000000\r\n"
            L"EndFragment:00000000\r\n"
            L"<html><body>\r\n"
            L"<!--StartFragment -->\r\n");

    copyStringEnd(buf, html);
    copyStringEnd(buf, L"\r\n");

    copyStringEnd(buf,
            L"<!--EndFragment-->\r\n"
            L"</body>\r\n"
            L"</html>");

    wchar_t *ptr = wcsstr(buf, L"StartHTML");
    wsprintfW(ptr + 10, L"%08u", wcsstr(buf, L"<html>") - buf);
    *(ptr + 10 + 8) = '\r';

    ptr = wcsstr(buf, L"EndHTML");
    wsprintfW(ptr + 8, L"%08u", wcslen(buf));
    *(ptr + 8 + 8) = '\r';

    ptr = wcsstr(buf, L"StartFragment");
    wsprintfW(ptr + 14, L"%08u", wcsstr(buf, L"<!--StartFrag") - buf);
    *(ptr + 14 + 8) = '\r';

    ptr = wcsstr(buf, L"EndFragment");
    wsprintfW(ptr + 12, L"%08u", wcsstr(buf, L"<!--EndFrag") - buf);
    *(ptr + 12 + 8) = '\r';



    if (OpenClipboard(NULL)) {
        EmptyClipboard();

        HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, wcslen(buf) * sizeof(wchar_t) + 4);

        wchar_t *ptrs = (wchar_t *)GlobalLock(hText);
        wcscpy_s(ptrs, wcslen(buf) + 2, buf);
        GlobalUnlock(hText);

        SetClipboardData(RegisterClipboardFormatA("HTML Format"), hText);
        CloseClipboard();
        GlobalFree(hText);
    }
    free(buf);
}
asdf
  • 3
  • 1
  • 2
    This is explained in the documentation http://msdn.microsoft.com/en-us/library/aa767917(v=vs.85).aspx "CF_HTML is entirely text format and uses the transformation format UTF-8." – Raymond Chen Jan 20 '14 at 00:41

1 Answers1

0

According to the documentation:

The only character set supported by the clipboard is Unicode in its UTF-8 encoding.

What you're calling "Unicode" is UTF-16-LE. Which is not UTF-8. If you try to interpret UTF-16-LE as UTF-8, it's going to look like it starts with "V\0", and then most code will just treat that \0 as the end of the string and stop reading.

You need to encode it to UTF-8—which is stored as char, not wchar_t—and paste that.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • if I used char is it UTF-8 automatically? If it's not how can I encode it? – asdf Jan 20 '14 at 02:48
  • 2
    Use `WideCharToMultiByte()` to convert a UTF-16 encoded `wchar_t` buffer to a UTF_8 encoded `char` buffer, then you can put the `char` buffer on the clipboard. – Remy Lebeau Jan 20 '14 at 03:07