From 5fdf5d795aaf459c419f5d0f1b67704cdb8d8db0 Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Wed, 7 Dec 2022 21:07:44 +0300
Subject: valid hipdi initialization

---
 src/mir_core/src/Windows/miranda.cpp | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

(limited to 'src/mir_core')

diff --git a/src/mir_core/src/Windows/miranda.cpp b/src/mir_core/src/Windows/miranda.cpp
index f7d8571191..36f75e4ea1 100644
--- a/src/mir_core/src/Windows/miranda.cpp
+++ b/src/mir_core/src/Windows/miranda.cpp
@@ -241,6 +241,27 @@ static void __cdecl compactHeapsThread(void*)
 	}
 }
 
+static void EnableDpiAware()
+{
+	if (HMODULE hInst = GetModuleHandleW(L"user32")) {
+		typedef void (WINAPI *pfnSetProcessDpiAwarenessContext)(HANDLE);
+		if (auto *pFunc = (pfnSetProcessDpiAwarenessContext)GetProcAddress(hInst, "SetProcessDpiAwarenessContext")) {
+			pFunc(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
+			return;
+		}
+
+		typedef void (WINAPI *pfnSetProcessDpiAwareness)(DWORD);
+		if (auto *pFunc = (pfnSetProcessDpiAwareness)GetProcAddress(hInst, "SetProcessDpiAwareness")) {
+			pFunc(2);
+			return;
+		}
+
+		typedef void (WINAPI *pfnSetProcessDPIAware_t)(void);
+		if (auto *pFunc = (pfnSetProcessDPIAware_t)GetProcAddress(hInst, "SetProcessDPIAware"))
+			pFunc();
+	}
+}
+
 MIR_CORE_DLL(void) BeginMessageLoop()
 {
 	_set_invalid_parameter_handler(&crtErrorHandler);
@@ -260,12 +281,8 @@ MIR_CORE_DLL(void) BeginMessageLoop()
 		bufferedPaintUninit = (pfnBufferedPaintUninit)GetProcAddress(hThemeAPI, "BufferedPaintUninit");
 	}
 
-	if (g_bEnableDpiAware) {
-		typedef BOOL (WINAPI *pfnSetProcessDPIAware_t)(void);
-		auto *pFunc = (pfnSetProcessDPIAware_t)GetProcAddress(GetModuleHandleW(L"user32"), "SetProcessDPIAware");
-		if (pFunc != nullptr)
-			pFunc();
-	}
+	if (g_bEnableDpiAware)
+		EnableDpiAware();
 
 	if (bufferedPaintInit)
 		bufferedPaintInit();
-- 
cgit v1.2.3