From 4b8867069b3b49cfabe2a7ef6275a7c0d85406af Mon Sep 17 00:00:00 2001 From: Nulano Date: Thu, 7 Nov 2024 22:06:28 +0100 Subject: [PATCH 1/3] Fix GetWindowDpiAwarenessContext NULL check --- src/display.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/display.c b/src/display.c index 89fec2c28..5babd4f2a 100644 --- a/src/display.c +++ b/src/display.c @@ -329,7 +329,7 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { HWND wnd; DWORD rop; PyObject *buffer; - HANDLE dpiAwareness; + HANDLE dpiAwareness = NULL; HMODULE user32; Func_GetWindowDpiAwarenessContext GetWindowDpiAwarenessContext_function; Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function; @@ -359,23 +359,15 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { user32 = LoadLibraryA("User32.dll"); SetThreadDpiAwarenessContext_function = (Func_SetThreadDpiAwarenessContext )GetProcAddress(user32, "SetThreadDpiAwarenessContext"); + GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext + )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (SetThreadDpiAwarenessContext_function != NULL) { - if (screens == -1) { - GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext - )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); - DPI_AWARENESS_CONTEXT dpiAwarenessContext = + if (screens == -1 && GetWindowDpiAwarenessContext_function != NULL) { + dpiAwareness = GetWindowDpiAwarenessContext_function(wnd); - if (GetWindowDpiAwarenessContext_function != NULL && - dpiAwarenessContext != NULL) { - dpiAwareness = - SetThreadDpiAwarenessContext_function(dpiAwarenessContext); - } else { - dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); - } - } else { - // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) - dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); } + // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) + dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwareness == NULL ? (HANDLE)-3 : dpiAwareness); } if (screens == 1) { From a6c941ac2c5e0b88d827c6d5c753cce3021e9e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Baranovi=C4=8D?= Date: Thu, 7 Nov 2024 22:22:02 +0100 Subject: [PATCH 2/3] Do not load GetWindowDpiAwarenessContext until its needed Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/display.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/display.c b/src/display.c index 5babd4f2a..353d52396 100644 --- a/src/display.c +++ b/src/display.c @@ -359,9 +359,9 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { user32 = LoadLibraryA("User32.dll"); SetThreadDpiAwarenessContext_function = (Func_SetThreadDpiAwarenessContext )GetProcAddress(user32, "SetThreadDpiAwarenessContext"); - GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext - )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (SetThreadDpiAwarenessContext_function != NULL) { + GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext + )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (screens == -1 && GetWindowDpiAwarenessContext_function != NULL) { dpiAwareness = GetWindowDpiAwarenessContext_function(wnd); From acba5c47f8be16869da92efb877f07451612fa04 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 8 Nov 2024 08:24:13 +1100 Subject: [PATCH 3/3] Lint fix --- src/display.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/display.c b/src/display.c index 353d52396..b0693bf71 100644 --- a/src/display.c +++ b/src/display.c @@ -363,11 +363,12 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (screens == -1 && GetWindowDpiAwarenessContext_function != NULL) { - dpiAwareness = - GetWindowDpiAwarenessContext_function(wnd); + dpiAwareness = GetWindowDpiAwarenessContext_function(wnd); } // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) - dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwareness == NULL ? (HANDLE)-3 : dpiAwareness); + dpiAwareness = SetThreadDpiAwarenessContext_function( + dpiAwareness == NULL ? (HANDLE)-3 : dpiAwareness + ); } if (screens == 1) {