From 69d2a988abd8e33d01b77cee25b5a4f26069b132 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Mon, 22 Jan 2024 11:37:45 +0800 Subject: [PATCH] handle unicode text for clipboard https://learn.microsoft.com/en-us/windows/win32/dataxchg/clipboard-formats#synthesized-clipboard-formats --- src/plugin.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/plugin.c b/src/plugin.c index 7d459d6..fd3fa69 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -57,13 +57,14 @@ static void set_clipboard(char *text) { if (!OpenClipboard(ctx->hwnd)) return; EmptyClipboard(); - HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1); + int len = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, len * sizeof(WCHAR)); if (hData != NULL) { - char *data = (char *)GlobalLock(hData); + WCHAR *data = (WCHAR *)GlobalLock(hData); if (data != NULL) { - strcpy(data, text); + MultiByteToWideChar(CP_UTF8, 0, text, -1, data, len); GlobalUnlock(hData); - SetClipboardData(CF_TEXT, hData); + SetClipboardData(CF_UNICODETEXT, hData); } } CloseClipboard();