From 187f9549b81b39401ceaa20a84e69dc9e662040a Mon Sep 17 00:00:00 2001 From: Rob Conde Date: Mon, 1 May 2023 11:12:27 -0400 Subject: [PATCH] To avoid process termination on `glfwInit` when running as a windows service, change joystick related dll loading to be lazy based on access of joystick functionality via the GLFW api. --- src/win32_init.c | 34 ---------------------------------- src/win32_joystick.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/win32_init.c b/src/win32_init.c index ef2615f1..8dd06285 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -106,40 +106,6 @@ static GLFWbool loadLibraries(void) _glfw.win32.user32.GetSystemMetricsForDpi_ = (PFN_GetSystemMetricsForDpi) _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "GetSystemMetricsForDpi"); - _glfw.win32.dinput8.instance = _glfwPlatformLoadModule("dinput8.dll"); - if (_glfw.win32.dinput8.instance) - { - _glfw.win32.dinput8.Create = (PFN_DirectInput8Create) - _glfwPlatformGetModuleSymbol(_glfw.win32.dinput8.instance, "DirectInput8Create"); - } - - { - int i; - const char* names[] = - { - "xinput1_4.dll", - "xinput1_3.dll", - "xinput9_1_0.dll", - "xinput1_2.dll", - "xinput1_1.dll", - NULL - }; - - for (i = 0; names[i]; i++) - { - _glfw.win32.xinput.instance = _glfwPlatformLoadModule(names[i]); - if (_glfw.win32.xinput.instance) - { - _glfw.win32.xinput.GetCapabilities = (PFN_XInputGetCapabilities) - _glfwPlatformGetModuleSymbol(_glfw.win32.xinput.instance, "XInputGetCapabilities"); - _glfw.win32.xinput.GetState = (PFN_XInputGetState) - _glfwPlatformGetModuleSymbol(_glfw.win32.xinput.instance, "XInputGetState"); - - break; - } - } - } - _glfw.win32.dwmapi.instance = _glfwPlatformLoadModule("dwmapi.dll"); if (_glfw.win32.dwmapi.instance) { diff --git a/src/win32_joystick.c b/src/win32_joystick.c index 4e83577a..9ad1854f 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -574,6 +574,45 @@ void _glfwDetectJoystickDisconnectionWin32(void) GLFWbool _glfwInitJoysticksWin32(void) { + // These dlls loads used to be in win32_init/loadLibraries but even + // loading these libraries can cause the process to terminate when + // running under a Windows service on some operating systems (e.g. + // Windows Server 2019). Thus, only load them if joystick functionality + // is requested by the application. + _glfw.win32.dinput8.instance = _glfwPlatformLoadModule("dinput8.dll"); + if (_glfw.win32.dinput8.instance) + { + _glfw.win32.dinput8.Create = (PFN_DirectInput8Create) + _glfwPlatformGetModuleSymbol(_glfw.win32.dinput8.instance, "DirectInput8Create"); + } + + { + int i; + const char* names[] = + { + "xinput1_4.dll", + "xinput1_3.dll", + "xinput9_1_0.dll", + "xinput1_2.dll", + "xinput1_1.dll", + NULL + }; + + for (i = 0; names[i]; i++) + { + _glfw.win32.xinput.instance = _glfwPlatformLoadModule(names[i]); + if (_glfw.win32.xinput.instance) + { + _glfw.win32.xinput.GetCapabilities = (PFN_XInputGetCapabilities) + _glfwPlatformGetModuleSymbol(_glfw.win32.xinput.instance, "XInputGetCapabilities"); + _glfw.win32.xinput.GetState = (PFN_XInputGetState) + _glfwPlatformGetModuleSymbol(_glfw.win32.xinput.instance, "XInputGetState"); + + break; + } + } + } + if (_glfw.win32.dinput8.instance) { if (FAILED(DirectInput8Create(_glfw.win32.instance,