diff --git a/win32/src/PyWinTypes.h b/win32/src/PyWinTypes.h index 259387390..9763cab6a 100644 --- a/win32/src/PyWinTypes.h +++ b/win32/src/PyWinTypes.h @@ -48,22 +48,6 @@ if (!(dict = PyModule_GetDict(module))) \ return NULL; -// clang-format off -#define PYWIN_BEGIN_LOAD_LIBRARY(NAME) \ -{ \ - DWORD lastErr = GetLastError(); \ - HMODULE hmodule = GetModuleHandle(TEXT(NAME)); \ - if (hmodule == NULL) \ - hmodule = LoadLibrary(TEXT(NAME)); \ - if (hmodule != NULL) { \ - SetLastError(lastErr); - - -#define PYWIN_END_LOAD_LIBRARY \ - } \ -} -// clang-format om - // Helpers for our types. // Macro to handle PyObject layout changes in Py3k #define PYWIN_OBJECT_HEAD PyVarObject_HEAD_INIT(NULL, 0) @@ -99,6 +83,8 @@ typedef Py_ssize_t Py_hash_t; #endif // _MSC_VER #endif // BUILD_PYWINTYPES +extern PYWINTYPES_EXPORT HMODULE PyWin_LibraryModule(char *name); + // Py3k uses memoryview object in place of buffer, and we don't yet. extern PYWINTYPES_EXPORT PyObject *PyBuffer_New(Py_ssize_t size); extern PYWINTYPES_EXPORT PyObject *PyBuffer_FromMemory(void *buf, Py_ssize_t size); diff --git a/win32/src/PyWinTypesmodule.cpp b/win32/src/PyWinTypesmodule.cpp index 1564f84d5..f83847512 100644 --- a/win32/src/PyWinTypesmodule.cpp +++ b/win32/src/PyWinTypesmodule.cpp @@ -26,6 +26,17 @@ extern PyObject *PyWinMethod_NewHKEY(PyObject *self, PyObject *args); extern BOOL _PyWinDateTime_Init(); extern BOOL _PyWinDateTime_PrepareModuleDict(PyObject *dict); +HMODULE PyWin_LibraryModule(char *name) +{ + DWORD lastErr = GetLastError(); + HMODULE hmodule = GetModuleHandleA(name); + if (hmodule == NULL) + hmodule = LoadLibraryA(name); + if (hmodule != NULL) + SetLastError(lastErr); + return hmodule; +} + // XXX - Needs py3k modernization! // For py3k, a function that returns new memoryview object instead of buffer. // ??? Byte array object is mutable, maybe just use that directly as a substitute ??? diff --git a/win32/src/win32apimodule.cpp b/win32/src/win32apimodule.cpp index be2b991ce..75d0ef16c 100644 --- a/win32/src/win32apimodule.cpp +++ b/win32/src/win32apimodule.cpp @@ -6261,12 +6261,14 @@ PYWIN_MODULE_INIT_FUNC(win32api) PyModule_AddIntConstant(module, "VS_FF_SPECIALBUILD", VS_FF_SPECIALBUILD); // clang-format off - PYWIN_BEGIN_LOAD_LIBRARY("secur32.dll") + HMODULE hmodule = PyWin_LibraryModule("secur32.dll"); + if (hmodule != NULL) { pfnGetUserNameEx = (GetUserNameExfunc)GetProcAddress(hmodule, "GetUserNameExW"); pfnGetComputerObjectName = (GetUserNameExfunc)GetProcAddress(hmodule, "GetComputerObjectNameW"); - PYWIN_END_LOAD_LIBRARY + } - PYWIN_BEGIN_LOAD_LIBRARY("kernel32.dll") + hmodule = PyWin_LibraryModule("kernel32.dll"); + if (hmodule != NULL) { pfnGetComputerNameEx = (GetComputerNameExfunc)GetProcAddress(hmodule, "GetComputerNameExW"); pfnGetLongPathNameA = (GetLongPathNameAfunc)GetProcAddress(hmodule, "GetLongPathNameA"); pfnGetLongPathNameW = (GetLongPathNameWfunc)GetProcAddress(hmodule, "GetLongPathNameW"); @@ -6279,10 +6281,10 @@ PYWIN_MODULE_INIT_FUNC(win32api) pfnSetDllDirectory = (SetDllDirectoryfunc)GetProcAddress(hmodule, "SetDllDirectoryW"); pfnSetSystemPowerState = (SetSystemPowerStatefunc)GetProcAddress(hmodule, "SetSystemPowerState"); pfnGetNativeSystemInfo = (GetNativeSystemInfofunc)GetProcAddress(hmodule, "GetNativeSystemInfo"); - PYWIN_END_LOAD_LIBRARY - + } - PYWIN_BEGIN_LOAD_LIBRARY("user32.dll") + hmodule = PyWin_LibraryModule("user32.dll"); + if (hmodule != NULL) { pfnEnumDisplayMonitors = (EnumDisplayMonitorsfunc)GetProcAddress(hmodule, "EnumDisplayMonitors"); pfnEnumDisplayDevices = (EnumDisplayDevicesfunc)GetProcAddress(hmodule, "EnumDisplayDevicesW"); pfnChangeDisplaySettingsEx = (ChangeDisplaySettingsExfunc)GetProcAddress(hmodule, "ChangeDisplaySettingsExW"); @@ -6292,9 +6294,10 @@ PYWIN_MODULE_INIT_FUNC(win32api) pfnGetMonitorInfo = (GetMonitorInfofunc)GetProcAddress(hmodule, "GetMonitorInfoW"); pfnEnumDisplaySettingsEx = (EnumDisplaySettingsExfunc)GetProcAddress(hmodule, "EnumDisplaySettingsExW"); pfnGetLastInputInfo = (GetLastInputInfofunc)GetProcAddress(hmodule, "GetLastInputInfo"); - PYWIN_END_LOAD_LIBRARY + } - PYWIN_BEGIN_LOAD_LIBRARY("Advapi32.dll") + hmodule = PyWin_LibraryModule("advapi32.dll"); + if (hmodule != NULL) { pfnRegRestoreKey = (RegRestoreKeyfunc)GetProcAddress(hmodule, "RegRestoreKeyW"); pfnRegSaveKeyEx = (RegSaveKeyExfunc)GetProcAddress(hmodule, "RegSaveKeyExW"); pfnRegCreateKeyTransacted = (RegCreateKeyTransactedfunc)GetProcAddress(hmodule, "RegCreateKeyTransactedW"); @@ -6305,7 +6308,7 @@ PYWIN_MODULE_INIT_FUNC(win32api) pfnRegDeleteTree = (RegDeleteTreefunc)GetProcAddress(hmodule, "RegDeleteTreeW"); pfnRegOpenCurrentUser = (RegOpenCurrentUserfunc)GetProcAddress(hmodule, "RegOpenCurrentUser"); pfnRegOverridePredefKey = (RegOverridePredefKeyfunc)GetProcAddress(hmodule, "RegOverridePredefKey"); - PYWIN_END_LOAD_LIBRARY + } // clang-format on PYWIN_MODULE_INIT_RETURN_SUCCESS; diff --git a/win32/src/win32gui.i b/win32/src/win32gui.i index 6704484fb..91600c226 100644 --- a/win32/src/win32gui.i +++ b/win32/src/win32gui.i @@ -266,7 +266,8 @@ for (PyMethodDef *pmd = win32guiMethods; pmd->ml_name; pmd++) ) pmd->ml_flags = METH_VARARGS | METH_KEYWORDS; -PYWIN_BEGIN_LOAD_LIBRARY("user32.dll") +HMODULE hmodule = PyWin_LibraryModule("user32.dll"); +if (hmodule != NULL) { pfnSetLayeredWindowAttributes = (SetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"SetLayeredWindowAttributes"); pfnGetLayeredWindowAttributes = (GetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"GetLayeredWindowAttributes"); pfnUpdateLayeredWindow = (UpdateLayeredWindowfunc)GetProcAddress(hmodule,"UpdateLayeredWindow"); @@ -274,9 +275,10 @@ PYWIN_BEGIN_LOAD_LIBRARY("user32.dll") pfnGetMenuInfo = (GetMenuInfofunc)GetProcAddress(hmodule,"GetMenuInfo"); pfnSetMenuInfo = (SetMenuInfofunc)GetProcAddress(hmodule,"SetMenuInfo"); pfnDrawTextW = (DrawTextWfunc)GetProcAddress(hmodule, "DrawTextW"); -PYWIN_END_LOAD_LIBRARY +} -PYWIN_BEGIN_LOAD_LIBRARY("gdi32.dll") +hmodule = PyWin_LibraryModule("gdi32.dll"); +if (hmodule != NULL) { pfnAngleArc = (AngleArcfunc)GetProcAddress(hmodule,"AngleArc"); pfnPlgBlt = (PlgBltfunc)GetProcAddress(hmodule,"PlgBlt"); pfnGetWorldTransform = (GetWorldTransformfunc)GetProcAddress(hmodule,"GetWorldTransform"); @@ -286,13 +288,14 @@ PYWIN_BEGIN_LOAD_LIBRARY("gdi32.dll") pfnMaskBlt = (MaskBltfunc)GetProcAddress(hmodule,"MaskBlt"); pfnGetLayout = (GetLayoutfunc)GetProcAddress(hmodule,"GetLayout"); pfnSetLayout = (SetLayoutfunc)GetProcAddress(hmodule,"SetLayout"); -PYWIN_END_LOAD_LIBRARY +} -PYWIN_BEGIN_LOAD_LIBRARY("msimg32.dll") +hmodule = PyWin_LibraryModule("msimg32.dll"); +if (hmodule != NULL) { pfnGradientFill = (GradientFillfunc)GetProcAddress(hmodule,"GradientFill"); pfnTransparentBlt = (TransparentBltfunc)GetProcAddress(hmodule,"TransparentBlt"); pfnAlphaBlend = (AlphaBlendfunc)GetProcAddress(hmodule,"AlphaBlend"); -PYWIN_END_LOAD_LIBRARY +} %} %{