Skip to content

Commit

Permalink
Win32: Fix using executable instance and not ours
Browse files Browse the repository at this point in the history
Operations that take an instance handle should be passed the handle of
whatever module we are inside instead of blindly passing the handle of
the executable.

This commit makes GLFW retrieve its own instance on initialization.

This makes the most difference for window classes, which are
per-instance.  Using the executable instance led to name conflicts if
there were several copies of GLFW in a single process.

Note that having this is still a bad idea unless you know what things to
avoid, and those things are mostly platform-specific.  This is partly
because the library wasn't designed for it and partly because it needs
to save, update and restore various per-process and per-session settings
like current context and video mode.

However, multiple simultaneous copies of GLFW in a single Win32 process
should now at least initialize, like is already the case on other
platforms.

Fixes glfw#469
Fixes glfw#1296
Fixes glfw#1395
Related to glfw#927
Related to glfw#1885
elmindreda committed Mar 17, 2022
1 parent 66a4882 commit 07a5518
Showing 6 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@ video tutorials.
- IntellectualKitty
- Aaron Jacobs
- Erik S. V. Jansson
- jjYBdx4IL
- Toni Jovanoski
- Arseny Kapoulkine
- Cem Karan
@@ -210,6 +211,7 @@ video tutorials.
- Jared Tiala
- Sergey Tikhomirov
- Arthur Tombs
- TronicLabs
- Ioannis Tsakpinis
- Samuli Tuomola
- Matthew Turner
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -214,6 +214,7 @@ information on what to include when reporting a bug.
scancode than `PrtSc` (#1993)
- [Win32] Bugfix: `GLFW_KEY_PAUSE` scancode from `glfwGetKeyScancode` did not
match event scancode (#1993)
- [Win32] Bugfix: Instance-local operations used executable instance (#469,#1296,#1395)
- [Cocoa] Added support for `VK_EXT_metal_surface` (#1619)
- [Cocoa] Added locating the Vulkan loader at runtime in an application bundle
- [Cocoa] Moved main menu creation to GLFW initialization time (#1649)
12 changes: 11 additions & 1 deletion src/win32_init.c
Original file line number Diff line number Diff line change
@@ -71,6 +71,16 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
//
static GLFWbool loadLibraries(void)
{
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(const WCHAR*) &_glfw,
(HMODULE*) &_glfw.win32.instance))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"Win32: Failed to retrieve own module handle");
return GLFW_FALSE;
}

_glfw.win32.user32.instance = _glfwPlatformLoadModule("user32.dll");
if (!_glfw.win32.user32.instance)
{
@@ -334,7 +344,7 @@ static GLFWbool createHelperWindow(void)
WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0, 1, 1,
NULL, NULL,
GetModuleHandleW(NULL),
_glfw.win32.instance,
NULL);

if (!_glfw.win32.helperWindowHandle)
2 changes: 1 addition & 1 deletion src/win32_joystick.c
Original file line number Diff line number Diff line change
@@ -574,7 +574,7 @@ GLFWbool _glfwInitJoysticksWin32(void)
{
if (_glfw.win32.dinput8.instance)
{
if (FAILED(DirectInput8Create(GetModuleHandleW(NULL),
if (FAILED(DirectInput8Create(_glfw.win32.instance,
DIRECTINPUT_VERSION,
&IID_IDirectInput8W,
(void**) &_glfw.win32.dinput8.api,
1 change: 1 addition & 0 deletions src/win32_platform.h
Original file line number Diff line number Diff line change
@@ -442,6 +442,7 @@ typedef struct _GLFWwindowWin32
//
typedef struct _GLFWlibraryWin32
{
HINSTANCE instance;
HWND helperWindowHandle;
HDEVNOTIFY deviceNotificationHandle;
int acquiredMonitorCount;
8 changes: 4 additions & 4 deletions src/win32_window.c
Original file line number Diff line number Diff line change
@@ -1321,7 +1321,7 @@ static int createNativeWindow(_GLFWwindow* window,
fullWidth, fullHeight,
NULL, // No parent window
NULL, // No window menu
GetModuleHandleW(NULL),
_glfw.win32.instance,
(LPVOID) wndconfig);

_glfw_free(wideTitle);
@@ -1429,7 +1429,7 @@ GLFWbool _glfwRegisterWindowClassWin32(void)
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) windowProc;
wc.hInstance = GetModuleHandleW(NULL);
wc.hInstance = _glfw.win32.instance;
wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
wc.lpszClassName = _GLFW_WNDCLASSNAME;

@@ -1459,7 +1459,7 @@ GLFWbool _glfwRegisterWindowClassWin32(void)
//
void _glfwUnregisterWindowClassWin32(void)
{
UnregisterClassW(_GLFW_WNDCLASSNAME, GetModuleHandleW(NULL));
UnregisterClassW(_GLFW_WNDCLASSNAME, _glfw.win32.instance);
}

int _glfwCreateWindowWin32(_GLFWwindow* window,
@@ -2447,7 +2447,7 @@ VkResult _glfwCreateWindowSurfaceWin32(VkInstance instance,

memset(&sci, 0, sizeof(sci));
sci.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
sci.hinstance = GetModuleHandleW(NULL);
sci.hinstance = _glfw.win32.instance;
sci.hwnd = window->win32.handle;

err = vkCreateWin32SurfaceKHR(instance, &sci, allocator, surface);

0 comments on commit 07a5518

Please sign in to comment.