This commit is contained in:
Camilla Berglund 2016-06-23 17:40:30 +02:00
parent 0ee8159f6c
commit 66149a29da

View File

@ -1546,8 +1546,8 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
{ {
int characterCount; int characterCount;
HANDLE stringHandle; HANDLE object;
void* clipboardBuffer; WCHAR* buffer;
characterCount = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0); characterCount = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
if (!characterCount) if (!characterCount)
@ -1557,42 +1557,43 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
return; return;
} }
stringHandle = GlobalAlloc(GMEM_MOVEABLE, characterCount * sizeof(WCHAR)); object = GlobalAlloc(GMEM_MOVEABLE, characterCount * sizeof(WCHAR));
if (!stringHandle) if (!object)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to allocate global handle for clipboard"); "Win32: Failed to allocate global handle for clipboard");
return; return;
} }
clipboardBuffer = GlobalLock(stringHandle); buffer = GlobalLock(object);
if (!clipboardBuffer) if (!buffer)
{ {
GlobalFree(stringHandle); GlobalFree(object);
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to lock global handle"); _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to lock global handle");
return; return;
} }
MultiByteToWideChar(CP_UTF8, 0, string, -1, clipboardBuffer, characterCount); MultiByteToWideChar(CP_UTF8, 0, string, -1, buffer, characterCount);
GlobalUnlock(stringHandle); GlobalUnlock(object);
if (!OpenClipboard(_glfw.win32.helperWindowHandle)) if (!OpenClipboard(_glfw.win32.helperWindowHandle))
{ {
GlobalFree(stringHandle); GlobalFree(object);
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard"); _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard");
return; return;
} }
EmptyClipboard(); EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, stringHandle); SetClipboardData(CF_UNICODETEXT, object);
CloseClipboard(); CloseClipboard();
} }
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{ {
HANDLE stringHandle; HANDLE object;
WCHAR* buffer;
if (!OpenClipboard(_glfw.win32.helperWindowHandle)) if (!OpenClipboard(_glfw.win32.helperWindowHandle))
{ {
@ -1600,8 +1601,8 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
return NULL; return NULL;
} }
stringHandle = GetClipboardData(CF_UNICODETEXT); object = GetClipboardData(CF_UNICODETEXT);
if (!stringHandle) if (!object)
{ {
CloseClipboard(); CloseClipboard();
@ -1610,11 +1611,20 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
return NULL; return NULL;
} }
buffer = GlobalLock(object);
if (!buffer)
{
CloseClipboard();
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to lock global handle");
return NULL;
}
free(_glfw.win32.clipboardString); free(_glfw.win32.clipboardString);
_glfw.win32.clipboardString = _glfw.win32.clipboardString =
_glfwCreateUTF8FromWideStringWin32(GlobalLock(stringHandle)); _glfwCreateUTF8FromWideStringWin32(buffer);
GlobalUnlock(stringHandle); GlobalUnlock(object);
CloseClipboard(); CloseClipboard();
if (!_glfw.win32.clipboardString) if (!_glfw.win32.clipboardString)