Win32: a failed RegisterClassW or CreateWindowExW shows a MessageBox and the constructor carries on with a null HWND #209

Open
opened 2026-11-27 09:06:21 +00:00 by jorijnvdgraaf · 0 comments

Summary

The Win32 constructor reports a failed window class registration and a failed CreateWindowExW with a modal MessageBox and carries on: it shows and updates a null hwnd and passes it to vkCreateWin32SurfaceKHR, whose valid usage asks for a valid HWND. It should throw at the failed step, which since e1ca2fd (#169) also releases what it had made. Line numbers refer to e1ca2fd.

Details

  • implementations/Crafter.Graphics-Window.cpp:448-449: if (!RegisterClassW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) shows MessageBox(NULL, "Window Class Registration Failed!", ...), and the constructor goes on to CreateWindowExW with that class (:455).
  • :467-468: if (hwnd == NULL) shows MessageBox(NULL, "Window Creation Failed!", ...), and the constructor goes on to ShowWindow(hwnd, SW_SHOWNORMAL) and UpdateWindow(hwnd) (:472-473), the message pump, and createInfo.hwnd = hwnd; into vkCreateWin32SurfaceKHR (:488-489).
  • VUID-VkWin32SurfaceCreateInfoKHR-hwnd-01308, chapters/VK_KHR_surface/wsi.adoc:350-351 at Vulkan-Docs v1.4.369: "hwnd must be a valid Win32 HWND". With a null one the outcome is the driver's: an error, which CheckVkResult throws and ~Window now cleans up after, or anything else.
  • So the application gets no error it can handle at the call that failed, and a modal dialog it did not ask for. Both calls also pass char strings to MessageBox, the A function (#186).

Found by reading the code, in the pre-push review of #169's fix; not reproduced.

Suggested fix

Throw a std::runtime_error naming the call and GetLastError() where RegisterClassW fails with anything but ERROR_CLASS_ALREADY_EXISTS and where CreateWindowExW returns null, and drop the two MessageBox calls. Since #169's fix the constructor delegates to Window(), so ~Window then releases what had been made; it skips a null hwnd.

## Summary The Win32 constructor reports a failed window class registration and a failed `CreateWindowExW` with a modal `MessageBox` and carries on: it shows and updates a null `hwnd` and passes it to `vkCreateWin32SurfaceKHR`, whose valid usage asks for a valid `HWND`. It should throw at the failed step, which since `e1ca2fd` (#169) also releases what it had made. Line numbers refer to `e1ca2fd`. ## Details - `implementations/Crafter.Graphics-Window.cpp:448-449`: `if (!RegisterClassW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)` shows `MessageBox(NULL, "Window Class Registration Failed!", ...)`, and the constructor goes on to `CreateWindowExW` with that class (`:455`). - `:467-468`: `if (hwnd == NULL)` shows `MessageBox(NULL, "Window Creation Failed!", ...)`, and the constructor goes on to `ShowWindow(hwnd, SW_SHOWNORMAL)` and `UpdateWindow(hwnd)` (`:472-473`), the message pump, and `createInfo.hwnd = hwnd;` into `vkCreateWin32SurfaceKHR` (`:488-489`). - VUID-VkWin32SurfaceCreateInfoKHR-hwnd-01308, `chapters/VK_KHR_surface/wsi.adoc:350-351` at Vulkan-Docs `v1.4.369`: "hwnd must be a valid Win32 HWND". With a null one the outcome is the driver's: an error, which `CheckVkResult` throws and `~Window` now cleans up after, or anything else. - So the application gets no error it can handle at the call that failed, and a modal dialog it did not ask for. Both calls also pass `char` strings to `MessageBox`, the A function (#186). Found by reading the code, in the pre-push review of #169's fix; not reproduced. ## Suggested fix Throw a `std::runtime_error` naming the call and `GetLastError()` where `RegisterClassW` fails with anything but `ERROR_CLASS_ALREADY_EXISTS` and where `CreateWindowExW` returns null, and drop the two `MessageBox` calls. Since #169's fix the constructor delegates to `Window()`, so `~Window` then releases what had been made; it skips a null `hwnd`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#209
No description provided.