From 164c9121ceafb4fad686470ed963a7bb14f15c63 Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 9 Jan 2023 16:01:44 +0000 Subject: [PATCH] Windows: Fix Vista/7 compatibility --- .../native/juce_win32_Windowing.cpp | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp index c7a78b54c37b..c2748e4bff27 100644 --- a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp @@ -501,12 +501,29 @@ static double getGlobalDPI() //============================================================================== class ScopedSuspendResumeNotificationRegistration { + static auto& getFunctions() + { + struct Functions + { + using Register = HPOWERNOTIFY (WINAPI*) (HANDLE, DWORD); + using Unregister = BOOL (WINAPI*) (HPOWERNOTIFY); + + Register registerNotification = (Register) getUser32Function ("RegisterSuspendResumeNotification"); + Unregister unregisterNotification = (Unregister) getUser32Function ("UnregisterSuspendResumeNotification"); + + bool isValid() const { return registerNotification != nullptr && unregisterNotification != nullptr; } + }; + + static Functions functions; + return functions; + } + public: ScopedSuspendResumeNotificationRegistration() = default; explicit ScopedSuspendResumeNotificationRegistration (HWND window) - : handle (SystemStats::getOperatingSystemType() >= SystemStats::Windows8_0 - ? RegisterSuspendResumeNotification (window, DEVICE_NOTIFY_WINDOW_HANDLE) + : handle (getFunctions().isValid() + ? getFunctions().registerNotification (window, DEVICE_NOTIFY_WINDOW_HANDLE) : nullptr) {} @@ -516,7 +533,7 @@ class ScopedSuspendResumeNotificationRegistration void operator() (HPOWERNOTIFY ptr) const { if (ptr != nullptr) - UnregisterSuspendResumeNotification (ptr); + getFunctions().unregisterNotification (ptr); } };