mhammond / pywin32

Python for Windows (pywin32) Extensions
4.9k stars 783 forks source link

Last error wrongly set by some modules #2302

Open CristiFati opened 6 days ago

CristiFati commented 6 days ago

This is a proposed fix for #2301.

When searching for functions in libraries, GetModuleHandle is attempted 1st (most likely to avoid incrementing RefCount), and only if it fails LoadLibrary is used. Unfortunately, for libraries that aren't already loaded ("secur32 for win32api or msimg32 for win32gui) GetModuleHandle sets last error flag to 126: ERROR_MOD_NOT_FOUND. Since the flag is not cleared, for win32gui my EnumWindows* changes seem to have no effect.

The proposed fix is in a form of a macro that does the library loading (following Py_BEGIN_ALLOW_THREADS model) and clears this particular error.

Notes: