From 0ee8159f6c57cfb07d66f78fef93a611119efc9d Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 23 Jun 2016 17:06:03 +0200 Subject: [PATCH] Win32: Remove intermediate clipboard string copy --- src/win32_window.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/win32_window.c b/src/win32_window.c index f46a44a6..e9c75d39 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1545,37 +1545,41 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) { - WCHAR* wideString; + int characterCount; HANDLE stringHandle; - size_t wideSize; + void* clipboardBuffer; - wideString = _glfwCreateWideStringFromUTF8Win32(string); - if (!wideString) + characterCount = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0); + if (!characterCount) { _glfwInputError(GLFW_PLATFORM_ERROR, - "Win32: Failed to convert string to UTF-16"); + "Win32: Failed to convert clipboard string to UTF-16"); return; } - wideSize = (wcslen(wideString) + 1) * sizeof(WCHAR); - - stringHandle = GlobalAlloc(GMEM_MOVEABLE, wideSize); + stringHandle = GlobalAlloc(GMEM_MOVEABLE, characterCount * sizeof(WCHAR)); if (!stringHandle) { - free(wideString); - _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to allocate global handle for clipboard"); return; } - memcpy(GlobalLock(stringHandle), wideString, wideSize); + clipboardBuffer = GlobalLock(stringHandle); + if (!clipboardBuffer) + { + GlobalFree(stringHandle); + + _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to lock global handle"); + return; + } + + MultiByteToWideChar(CP_UTF8, 0, string, -1, clipboardBuffer, characterCount); GlobalUnlock(stringHandle); if (!OpenClipboard(_glfw.win32.helperWindowHandle)) { GlobalFree(stringHandle); - free(wideString); _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard"); return; @@ -1584,8 +1588,6 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) EmptyClipboard(); SetClipboardData(CF_UNICODETEXT, stringHandle); CloseClipboard(); - - free(wideString); } const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)