From fcc7206ff01b110806f01cf6e7cd24613dd43503 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 5 Aug 2024 17:56:08 +0100 Subject: [PATCH] Handle GetWindowLongPtr errors as advised by Win32 docs --- src/vsg/platform/win32/Win32_Window.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/vsg/platform/win32/Win32_Window.cpp b/src/vsg/platform/win32/Win32_Window.cpp index 67d2c87d7d..fe1a05248e 100644 --- a/src/vsg/platform/win32/Win32_Window.cpp +++ b/src/vsg/platform/win32/Win32_Window.cpp @@ -454,7 +454,11 @@ Win32_Window::Win32_Window(vsg::ref_ptr traits) : if (_window == nullptr) throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) failed to create Window, CreateWindowEx did not return a valid window handle : "); // set window handle user data pointer to hold ref to this so we can retrieve in WindowsProc - if (!SetWindowLongPtr(_window, GWLP_USERDATA, reinterpret_cast(this))) throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) SetWindowLongPtr(..) failed : "); + // this function lies about failing so we must jump through some hoops + // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptra#return-value + SetLastError(NOERROR); + if (!SetWindowLongPtr(_window, GWLP_USERDATA, reinterpret_cast(this)) && GetLastError() != NOERROR) + throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) SetWindowLongPtr(..) failed : "); // reposition once the window has been created to account for borders etc if (!::SetWindowPos(_window, nullptr, screenx, screeny, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, 0))