Kalmat / PyMonCtl

Cross-Platform module which provides a set of features to get info on and control monitors/screens/displays.
BSD 3-Clause "New" or "Revised" License
5 stars 3 forks source link

The library fails under the Windows Server #4

Closed poipoiPIO closed 4 months ago

poipoiPIO commented 1 year ago

Description

It looks like the library actively uses GetAwarenessFromDpiAwarenessContext function of winapi, which is unavailable on any version of windows server. And when it comes to this line, execution is failing with AttributeError: function 'GetAwarenessFromDpiAwarenessContext' not found exception.

Possible Solution

Wrap the GetAwarenessFromDpiAwarenessContext call into the try-except clause on attribute error and assign zero to dpiAware variable here:

try:
    dpiAware = ctypes.windll.user32.GetAwarenessFromDpiAwarenessContext(ctypes.windll.user32.GetThreadDpiAwarenessContext())
except AttributeError:
    dpiAware = 0

Also:

Looks like here is also this issue with PyWinBox library

If the try-catch workaround is okay, I could suggest a pull request with changes Best regards, Lappely <3

Kalmat commented 1 year ago

Hi! Thank you SO MUCH for your feedback and help! I didn't test this in Windows Server, sorry, so thank you for finding and pointing out this issue! I fully agree with your proposed solution in both, PyMonCtl and PyWinBox.

There is something I would like to know/test in addition: after getting current DpiAwareness value and, in case it is 0, the module tries to set it to 2, using ctypes.windll.shcore.SetProcessDpiAwareness(2)... Will this sentence work in Windows Server, or should it be moved inside the try-except block as well?

Thank you again!

poipoiPIO commented 1 year ago

Thank you for your answer!

ctypes.windll.shcore.SetProcessDpiAwareness(2) works well on Windows Server 2012. As it mentioned here there should not be any troubles with Windows Server 2012 R2 and higher. ^^"