From 29c198473334805afa664d6e12da9a0d67588592 Mon Sep 17 00:00:00 2001 From: Cristi Fati Date: Fri, 25 Oct 2024 04:17:28 +0300 Subject: [PATCH] Last error wrongly set by some modules (#2302) --- CHANGES.txt | 1 + win32/src/PyWinTypes.h | 2 + win32/src/PyWinTypesmodule.cpp | 17 +++++-- win32/src/win32apimodule.cpp | 24 +++------- win32/src/win32consolemodule.cpp | 4 +- win32/src/win32file.i | 25 ++++------ win32/src/win32gui.i | 66 ++++++++++++--------------- win32/src/win32inet.i | 2 +- win32/src/win32job.i | 10 ++-- win32/src/win32net/win32netmodule.cpp | 4 +- win32/src/win32pipe.i | 8 ++-- win32/src/win32process.i | 27 ++++------- win32/src/win32profilemodule.cpp | 5 +- win32/src/win32service.i | 9 ++-- win32/src/win32transactionmodule.cpp | 6 +-- win32/src/win32tsmodule.cpp | 12 ++--- 16 files changed, 92 insertions(+), 130 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d02c6fcae..c66d05f48 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -14,6 +14,7 @@ https://mhammond.github.io/pywin32_installers.html. Coming in build 309, as yet unreleased -------------------------------------- +* Last error wrongly set by some modules (#2302, @CristiFati) * Dropped support for Python 3.7 (#2207, @Avasam) * Implement the creation of SAFEARRAY(VT_RECORD) from a sequence of COM Records (#2317, @geppi) * Implement record pointers as [in, out] method parameters of a Dispatch Interface (#2304, #2310, @geppi) diff --git a/win32/src/PyWinTypes.h b/win32/src/PyWinTypes.h index 620aada9c..cd0fe7bdb 100644 --- a/win32/src/PyWinTypes.h +++ b/win32/src/PyWinTypes.h @@ -83,6 +83,8 @@ typedef Py_ssize_t Py_hash_t; #endif // _MSC_VER #endif // BUILD_PYWINTYPES +extern PYWINTYPES_EXPORT HMODULE PyWin_GetOrLoadLibraryHandle(const 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..e095fdf93 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_GetOrLoadLibraryHandle(const 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 ??? @@ -970,10 +981,8 @@ extern "C" __declspec(dllexport) BOOL WINAPI DllMain(HANDLE hInstance, DWORD dwR { FARPROC fp; // dll usually will already be loaded - HMODULE hmodule = GetModuleHandle(_T("AdvAPI32.dll")); - if (hmodule == NULL) - hmodule = LoadLibrary(_T("AdvAPI32.dll")); - if (hmodule) { + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("advapi32.dll"); + if (hmodule != NULL) { fp = GetProcAddress(hmodule, "AddAccessAllowedAce"); if (fp) addaccessallowedace = (addacefunc)(fp); diff --git a/win32/src/win32apimodule.cpp b/win32/src/win32apimodule.cpp index 026816e69..45d8d532e 100644 --- a/win32/src/win32apimodule.cpp +++ b/win32/src/win32apimodule.cpp @@ -4772,14 +4772,12 @@ static PyObject *PySetSystemTime(PyObject *self, PyObject *args) )) return NULL; PyW32_BEGIN_ALLOW_THREADS result = ::SetSystemTime(&t); - PyW32_END_ALLOW_THREADS + PyW32_END_ALLOW_THREADS; - if (!result) - { + if (!result) { return ReturnAPIError("SetSystemTime"); } - else - { + else { return Py_BuildValue("i", result); } } @@ -6263,17 +6261,13 @@ PYWIN_MODULE_INIT_FUNC(win32api) PyModule_AddIntConstant(module, "VS_FF_PRIVATEBUILD", VS_FF_PRIVATEBUILD); PyModule_AddIntConstant(module, "VS_FF_SPECIALBUILD", VS_FF_SPECIALBUILD); - HMODULE hmodule = GetModuleHandle(TEXT("secur32.dll")); - if (hmodule == NULL) - hmodule = LoadLibrary(TEXT("secur32.dll")); + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("secur32.dll"); if (hmodule != NULL) { pfnGetUserNameEx = (GetUserNameExfunc)GetProcAddress(hmodule, "GetUserNameExW"); pfnGetComputerObjectName = (GetUserNameExfunc)GetProcAddress(hmodule, "GetComputerObjectNameW"); } - hmodule = GetModuleHandle(TEXT("kernel32.dll")); - if (hmodule == NULL) - hmodule = LoadLibrary(TEXT("kernel32.dll")); + hmodule = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); if (hmodule != NULL) { pfnGetComputerNameEx = (GetComputerNameExfunc)GetProcAddress(hmodule, "GetComputerNameExW"); pfnGetLongPathNameA = (GetLongPathNameAfunc)GetProcAddress(hmodule, "GetLongPathNameA"); @@ -6289,9 +6283,7 @@ PYWIN_MODULE_INIT_FUNC(win32api) pfnGetNativeSystemInfo = (GetNativeSystemInfofunc)GetProcAddress(hmodule, "GetNativeSystemInfo"); } - hmodule = GetModuleHandle(TEXT("user32.dll")); - if (hmodule == NULL) - hmodule = LoadLibrary(TEXT("user32.dll")); + hmodule = PyWin_GetOrLoadLibraryHandle("user32.dll"); if (hmodule != NULL) { pfnEnumDisplayMonitors = (EnumDisplayMonitorsfunc)GetProcAddress(hmodule, "EnumDisplayMonitors"); pfnEnumDisplayDevices = (EnumDisplayDevicesfunc)GetProcAddress(hmodule, "EnumDisplayDevicesW"); @@ -6304,9 +6296,7 @@ PYWIN_MODULE_INIT_FUNC(win32api) pfnGetLastInputInfo = (GetLastInputInfofunc)GetProcAddress(hmodule, "GetLastInputInfo"); } - hmodule = GetModuleHandle(TEXT("Advapi32.dll")); - if (hmodule == NULL) - hmodule = LoadLibrary(TEXT("Advapi32.dll")); + hmodule = PyWin_GetOrLoadLibraryHandle("advapi32.dll"); if (hmodule != NULL) { pfnRegRestoreKey = (RegRestoreKeyfunc)GetProcAddress(hmodule, "RegRestoreKeyW"); pfnRegSaveKeyEx = (RegSaveKeyExfunc)GetProcAddress(hmodule, "RegSaveKeyExW"); diff --git a/win32/src/win32consolemodule.cpp b/win32/src/win32consolemodule.cpp index daae981cf..48e84d727 100644 --- a/win32/src/win32consolemodule.cpp +++ b/win32/src/win32consolemodule.cpp @@ -2079,9 +2079,7 @@ PYWIN_MODULE_INIT_FUNC(win32console) PyDict_SetItemString(dict, "error", PyWinExc_ApiError); // load function pointers - kernel32_dll = GetModuleHandle(L"kernel32.dll"); - if (kernel32_dll == NULL) - kernel32_dll = LoadLibrary(L"kernel32.dll"); + kernel32_dll = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); if (kernel32_dll != NULL) { pfnGetConsoleProcessList = (GetConsoleProcessListfunc)GetProcAddress(kernel32_dll, "GetConsoleProcessList"); pfnGetConsoleDisplayMode = (GetConsoleDisplayModefunc)GetProcAddress(kernel32_dll, "GetConsoleDisplayMode"); diff --git a/win32/src/win32file.i b/win32/src/win32file.i index 714aed513..716b76262 100644 --- a/win32/src/win32file.i +++ b/win32/src/win32file.i @@ -5932,11 +5932,8 @@ PyCFunction pfnpy_OpenFileById=(PyCFunction)py_OpenFileById; ) pmd->ml_flags = METH_VARARGS | METH_KEYWORDS; - HMODULE hmodule; - hmodule=GetModuleHandle(TEXT("AdvAPI32.dll")); - if (hmodule==NULL) - hmodule=LoadLibrary(TEXT("AdvAPI32.dll")); - if (hmodule){ + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("advapi32.dll"); + if (hmodule != NULL) { pfnEncryptFile=(EncryptFilefunc)GetProcAddress(hmodule, "EncryptFileW"); pfnDecryptFile=(DecryptFilefunc)GetProcAddress(hmodule, "DecryptFileW"); pfnEncryptionDisable=(EncryptionDisablefunc)GetProcAddress(hmodule, "EncryptionDisable"); @@ -5952,12 +5949,10 @@ PyCFunction pfnpy_OpenFileById=(PyCFunction)py_OpenFileById; pfnReadEncryptedFileRaw=(ReadEncryptedFileRawfunc)GetProcAddress(hmodule, "ReadEncryptedFileRaw"); pfnWriteEncryptedFileRaw=(WriteEncryptedFileRawfunc)GetProcAddress(hmodule, "WriteEncryptedFileRaw"); pfnCloseEncryptedFileRaw=(CloseEncryptedFileRawfunc)GetProcAddress(hmodule, "CloseEncryptedFileRaw"); - } + } - hmodule=GetModuleHandle(TEXT("kernel32.dll")); - if (hmodule==NULL) - hmodule=LoadLibrary(TEXT("kernel32.dll")); - if (hmodule){ + hmodule = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); + if (hmodule != NULL) { pfnSetVolumeMountPoint=(SetVolumeMountPointfunc)GetProcAddress(hmodule, "SetVolumeMountPointW"); pfnDeleteVolumeMountPoint=(DeleteVolumeMountPointfunc)GetProcAddress(hmodule, "DeleteVolumeMountPointW"); pfnGetVolumeNameForVolumeMountPoint=(GetVolumeNameForVolumeMountPointfunc)GetProcAddress(hmodule, "GetVolumeNameForVolumeMountPointW"); @@ -6002,15 +5997,13 @@ PyCFunction pfnpy_OpenFileById=(PyCFunction)py_OpenFileById; pfnWow64RevertWow64FsRedirection=(Wow64RevertWow64FsRedirectionfunc)GetProcAddress(hmodule, "Wow64RevertWow64FsRedirection"); pfnReOpenFile=(ReOpenFilefunc)GetProcAddress(hmodule, "ReOpenFile"); pfnOpenFileById=(OpenFileByIdfunc)GetProcAddress(hmodule, "OpenFileById"); - } + } - hmodule=GetModuleHandle(TEXT("sfc.dll")); - if (hmodule==NULL) - hmodule=LoadLibrary(TEXT("sfc.dll")); - if (hmodule){ + hmodule = PyWin_GetOrLoadLibraryHandle("sfc.dll"); + if (hmodule != NULL) { pfnSfcGetNextProtectedFile=(SfcGetNextProtectedFilefunc)GetProcAddress(hmodule, "SfcGetNextProtectedFile"); pfnSfcIsFileProtected=(SfcIsFileProtectedfunc)GetProcAddress(hmodule, "SfcIsFileProtected"); - } + } %} diff --git a/win32/src/win32gui.i b/win32/src/win32gui.i index 518e84cf9..fa0906b7e 100644 --- a/win32/src/win32gui.i +++ b/win32/src/win32gui.i @@ -266,42 +266,36 @@ for (PyMethodDef *pmd = win32guiMethods; pmd->ml_name; pmd++) ) pmd->ml_flags = METH_VARARGS | METH_KEYWORDS; -HMODULE hmodule=GetModuleHandle(TEXT("user32.dll")); -if (hmodule==NULL) - hmodule=LoadLibrary(TEXT("user32.dll")); -if (hmodule){ - pfnSetLayeredWindowAttributes=(SetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"SetLayeredWindowAttributes"); - pfnGetLayeredWindowAttributes=(GetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"GetLayeredWindowAttributes"); - pfnUpdateLayeredWindow=(UpdateLayeredWindowfunc)GetProcAddress(hmodule,"UpdateLayeredWindow"); - pfnAnimateWindow=(AnimateWindowfunc)GetProcAddress(hmodule,"AnimateWindow"); - pfnGetMenuInfo=(GetMenuInfofunc)GetProcAddress(hmodule,"GetMenuInfo"); - pfnSetMenuInfo=(SetMenuInfofunc)GetProcAddress(hmodule,"SetMenuInfo"); - pfnDrawTextW=(DrawTextWfunc)GetProcAddress(hmodule, "DrawTextW"); - } - -hmodule=GetModuleHandle(TEXT("gdi32.dll")); -if (hmodule==NULL) - hmodule=LoadLibrary(TEXT("gdi32.dll")); -if (hmodule){ - pfnAngleArc=(AngleArcfunc)GetProcAddress(hmodule,"AngleArc"); - pfnPlgBlt=(PlgBltfunc)GetProcAddress(hmodule,"PlgBlt"); - pfnGetWorldTransform=(GetWorldTransformfunc)GetProcAddress(hmodule,"GetWorldTransform"); - pfnSetWorldTransform=(SetWorldTransformfunc)GetProcAddress(hmodule,"SetWorldTransform"); - pfnModifyWorldTransform=(ModifyWorldTransformfunc)GetProcAddress(hmodule,"ModifyWorldTransform"); - pfnCombineTransform=(CombineTransformfunc)GetProcAddress(hmodule,"CombineTransform"); - pfnMaskBlt=(MaskBltfunc)GetProcAddress(hmodule,"MaskBlt"); - pfnGetLayout=(GetLayoutfunc)GetProcAddress(hmodule,"GetLayout"); - pfnSetLayout=(SetLayoutfunc)GetProcAddress(hmodule,"SetLayout"); - } - -hmodule=GetModuleHandle(TEXT("msimg32.dll")); -if (hmodule==NULL) - hmodule=LoadLibrary(TEXT("msimg32.dll")); -if (hmodule){ - pfnGradientFill=(GradientFillfunc)GetProcAddress(hmodule,"GradientFill"); - pfnTransparentBlt=(TransparentBltfunc)GetProcAddress(hmodule,"TransparentBlt"); - pfnAlphaBlend=(AlphaBlendfunc)GetProcAddress(hmodule,"AlphaBlend"); - } +HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("user32.dll"); +if (hmodule != NULL) { + pfnSetLayeredWindowAttributes = (SetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"SetLayeredWindowAttributes"); + pfnGetLayeredWindowAttributes = (GetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"GetLayeredWindowAttributes"); + pfnUpdateLayeredWindow = (UpdateLayeredWindowfunc)GetProcAddress(hmodule,"UpdateLayeredWindow"); + pfnAnimateWindow = (AnimateWindowfunc)GetProcAddress(hmodule,"AnimateWindow"); + pfnGetMenuInfo = (GetMenuInfofunc)GetProcAddress(hmodule,"GetMenuInfo"); + pfnSetMenuInfo = (SetMenuInfofunc)GetProcAddress(hmodule,"SetMenuInfo"); + pfnDrawTextW = (DrawTextWfunc)GetProcAddress(hmodule, "DrawTextW"); +} + +hmodule = PyWin_GetOrLoadLibraryHandle("gdi32.dll"); +if (hmodule != NULL) { + pfnAngleArc = (AngleArcfunc)GetProcAddress(hmodule,"AngleArc"); + pfnPlgBlt = (PlgBltfunc)GetProcAddress(hmodule,"PlgBlt"); + pfnGetWorldTransform = (GetWorldTransformfunc)GetProcAddress(hmodule,"GetWorldTransform"); + pfnSetWorldTransform = (SetWorldTransformfunc)GetProcAddress(hmodule,"SetWorldTransform"); + pfnModifyWorldTransform = (ModifyWorldTransformfunc)GetProcAddress(hmodule,"ModifyWorldTransform"); + pfnCombineTransform = (CombineTransformfunc)GetProcAddress(hmodule,"CombineTransform"); + pfnMaskBlt = (MaskBltfunc)GetProcAddress(hmodule,"MaskBlt"); + pfnGetLayout = (GetLayoutfunc)GetProcAddress(hmodule,"GetLayout"); + pfnSetLayout = (SetLayoutfunc)GetProcAddress(hmodule,"SetLayout"); +} + +hmodule = PyWin_GetOrLoadLibraryHandle("msimg32.dll"); +if (hmodule != NULL) { + pfnGradientFill = (GradientFillfunc)GetProcAddress(hmodule,"GradientFill"); + pfnTransparentBlt = (TransparentBltfunc)GetProcAddress(hmodule,"TransparentBlt"); + pfnAlphaBlend = (AlphaBlendfunc)GetProcAddress(hmodule,"AlphaBlend"); +} %} %{ diff --git a/win32/src/win32inet.i b/win32/src/win32inet.i index 33d5afa51..e9edf6c52 100644 --- a/win32/src/win32inet.i +++ b/win32/src/win32inet.i @@ -1941,7 +1941,7 @@ extern PyObject *PyWinHttpOpen(PyObject *, PyObject *); %init %{ PyDict_SetItemString(d, "error", PyWinExc_ApiError); - HMODULE hmod = GetModuleHandle(TEXT("wininet.dll")); + HMODULE hmod = PyWin_GetOrLoadLibraryHandle("wininet.dll"); assert(hmod); PyWin_RegisterErrorMessageModule(INTERNET_ERROR_BASE, INTERNET_ERROR_LAST, diff --git a/win32/src/win32job.i b/win32/src/win32job.i index 1b3af02e2..ccf0faf38 100644 --- a/win32/src/win32job.i +++ b/win32/src/win32job.i @@ -20,12 +20,10 @@ static IsProcessInJobfunc pfnIsProcessInJob=NULL; %init %{ -HMODULE hmodule=GetModuleHandle(L"kernel32.dll"); -if (hmodule==NULL) - hmodule=LoadLibrary(L"kernel32.dll"); -if (hmodule){ - pfnIsProcessInJob=(IsProcessInJobfunc)GetProcAddress(hmodule,"IsProcessInJob"); - } +HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); +if (hmodule != NULL) { + pfnIsProcessInJob = (IsProcessInJobfunc)GetProcAddress(hmodule, "IsProcessInJob"); +} %} diff --git a/win32/src/win32net/win32netmodule.cpp b/win32/src/win32net/win32netmodule.cpp index 1698bfb6f..e4b7ba108 100644 --- a/win32/src/win32net/win32netmodule.cpp +++ b/win32/src/win32net/win32netmodule.cpp @@ -1211,10 +1211,8 @@ PYWIN_MODULE_INIT_FUNC(win32net) AddConstant(dict, "USE_FORCE", USE_FORCE); AddConstant(dict, "USE_LOTS_OF_FORCE", USE_LOTS_OF_FORCE); - HMODULE hmodule = GetModuleHandle(_T("netapi32")); + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("netapi32.dll"); #if WINVER >= 0x0500 - if (hmodule == NULL) - hmodule = LoadLibrary(_T("netapi32")); if (hmodule != NULL) { pfnNetValidateName = (NetValidateNamefunc)GetProcAddress(hmodule, "NetValidateName"); pfnNetGetJoinInformation = (NetGetJoinInformationfunc)GetProcAddress(hmodule, "NetGetJoinInformation"); diff --git a/win32/src/win32pipe.i b/win32/src/win32pipe.i index 5311900c4..f29125c7c 100644 --- a/win32/src/win32pipe.i +++ b/win32/src/win32pipe.i @@ -25,15 +25,13 @@ static GetNamedPipeClientProcessIdfunc pfnGetNamedPipeServerSessionId = NULL; // All errors raised by this module are of this type. PyDict_SetItemString(d, "error", PyWinExc_ApiError); - HMODULE hmod=GetModuleHandle(_T("Kernel32.dll")); - if (!hmod) - hmod=LoadLibrary(_T("Kernel32.dll")); - if (hmod){ + HMODULE hmod = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); + if (hmod != NULL) { pfnGetNamedPipeClientProcessId = (GetNamedPipeClientProcessIdfunc)GetProcAddress(hmod, "GetNamedPipeClientProcessId"); pfnGetNamedPipeServerProcessId = (GetNamedPipeClientProcessIdfunc)GetProcAddress(hmod, "GetNamedPipeServerProcessId"); pfnGetNamedPipeClientSessionId = (GetNamedPipeClientProcessIdfunc)GetProcAddress(hmod, "GetNamedPipeClientSessionId"); pfnGetNamedPipeServerSessionId = (GetNamedPipeClientProcessIdfunc)GetProcAddress(hmod, "GetNamedPipeServerSessionId"); - } + } %} %{ diff --git a/win32/src/win32process.i b/win32/src/win32process.i index 331ed0f23..c938f4566 100644 --- a/win32/src/win32process.i +++ b/win32/src/win32process.i @@ -1686,23 +1686,18 @@ PyObject *PyWriteProcessMemory(PyObject *self, PyObject *args) if (PyType_Ready(&PySTARTUPINFOType) == -1) return NULL; - FARPROC fp=NULL; - HMODULE hmodule=NULL; - hmodule=GetModuleHandle(_T("Psapi.dll")); - if (hmodule==NULL) - hmodule=LoadLibrary(_T("Psapi.dll")); - if (hmodule!=NULL){ + FARPROC fp = NULL; + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("psapi.dll"); + if (hmodule != NULL) { pfnEnumProcesses = (EnumProcessesfunc)GetProcAddress(hmodule, "EnumProcesses"); pfnEnumProcessModules = (EnumProcessModulesfunc)GetProcAddress(hmodule, "EnumProcessModules"); pfnEnumProcessModulesEx = (EnumProcessModulesExfunc)GetProcAddress(hmodule, "EnumProcessModulesEx"); pfnGetModuleFileNameEx = (GetModuleFileNameExfunc)GetProcAddress(hmodule, "GetModuleFileNameExW"); pfnGetProcessMemoryInfo = (GetProcessMemoryInfofunc)GetProcAddress(hmodule, "GetProcessMemoryInfo"); - } + } - hmodule=GetModuleHandle(_T("Kernel32.dll")); - if (hmodule==NULL) - hmodule=LoadLibrary(_T("Kernel32.dll")); - if (hmodule!=NULL){ + hmodule = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); + if (hmodule != NULL) { pfnGetProcessTimes=(GetProcessTimesfunc)GetProcAddress(hmodule,"GetProcessTimes"); pfnGetProcessIoCounters=(GetProcessIoCountersfunc)GetProcAddress(hmodule,"GetProcessIoCounters"); pfnGetProcessShutdownParameters=(GetProcessShutdownParametersfunc)GetProcAddress(hmodule,"GetProcessShutdownParameters"); @@ -1720,15 +1715,13 @@ PyObject *PyWriteProcessMemory(PyObject *self, PyObject *args) pfnSetProcessAffinityMask=(SetProcessAffinityMaskfunc)GetProcAddress(hmodule,"SetProcessAffinityMask"); pfnGetProcessId=(GetProcessIdfunc)GetProcAddress(hmodule, "GetProcessId"); pfnIsWow64Process=(IsWow64Processfunc)GetProcAddress(hmodule, "IsWow64Process"); - } + } - hmodule=GetModuleHandle(_T("User32.dll")); - if (hmodule==NULL) - hmodule=LoadLibrary(_T("User32.dll")); - if (hmodule!=NULL){ + hmodule = PyWin_GetOrLoadLibraryHandle("user32.dll"); + if (hmodule != NULL) { pfnGetProcessWindowStation=(GetProcessWindowStationfunc)GetProcAddress(hmodule,"GetProcessWindowStation"); pfnGetGuiResources=(GetGuiResourcesfunc)GetProcAddress(hmodule,"GetGuiResources"); - } + } // *sob* - these symbols don't exist in the platform sdk needed to build // using Python 2.3 diff --git a/win32/src/win32profilemodule.cpp b/win32/src/win32profilemodule.cpp index 17299f00b..98c4fac72 100644 --- a/win32/src/win32profilemodule.cpp +++ b/win32/src/win32profilemodule.cpp @@ -489,10 +489,7 @@ PYWIN_MODULE_INIT_FUNC(win32profile) PyModule_AddIntConstant(module, "PT_ROAMING", PT_ROAMING); PyModule_AddIntConstant(module, "PT_TEMPORARY", PT_TEMPORARY); - HMODULE hmodule; - hmodule = GetModuleHandle(L"Userenv.dll"); - if (hmodule == NULL) - hmodule = LoadLibrary(L"Userenv.dll"); + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("userenv.dll"); if (hmodule != NULL) { pfnDeleteProfile = (DeleteProfilefunc)GetProcAddress(hmodule, "DeleteProfileW"); pfnExpandEnvironmentStringsForUser = diff --git a/win32/src/win32service.i b/win32/src/win32service.i index 1150dff74..b91a0e16b 100644 --- a/win32/src/win32service.i +++ b/win32/src/win32service.i @@ -36,12 +36,9 @@ EnumServicesStatusExfunc fpEnumServicesStatusEx=NULL; PyDict_SetItemString(d, "error", PyWinExc_ApiError); PyDict_SetItemString(d, "HWINSTAType", (PyObject *)&PyHWINSTAType); PyDict_SetItemString(d, "HDESKType", (PyObject *)&PyHDESKType); - HMODULE hmod; FARPROC fp; - hmod=GetModuleHandle(_T("Advapi32")); - if (hmod==NULL) - hmod=LoadLibrary(_T("Advapi32")); - if (hmod!=NULL){ + HMODULE hmod = PyWin_GetOrLoadLibraryHandle("advapi32.dll"); + if (hmod != NULL) { fp=GetProcAddress(hmod,"QueryServiceStatusEx"); if (fp!=NULL) fpQueryServiceStatusEx=(QueryServiceStatusExfunc)fp; @@ -54,7 +51,7 @@ EnumServicesStatusExfunc fpEnumServicesStatusEx=NULL; fp=GetProcAddress(hmod,"EnumServicesStatusExW"); if (fp!=NULL) fpEnumServicesStatusEx=(EnumServicesStatusExfunc)fp; - } + } %} %{ diff --git a/win32/src/win32transactionmodule.cpp b/win32/src/win32transactionmodule.cpp index 27c62d606..df39f67a1 100644 --- a/win32/src/win32transactionmodule.cpp +++ b/win32/src/win32transactionmodule.cpp @@ -235,10 +235,8 @@ PYWIN_MODULE_INIT_FUNC(win32transaction) " transacted NTFS and transacted registry functions."); // Load dll and function pointers to avoid dependency on newer libraries and headers - HMODULE hmodule = GetModuleHandle(L"Ktmw32.dll"); - if (hmodule == NULL) - hmodule = LoadLibrary(L"Ktmw32.dll"); - if (hmodule) { + HMODULE hmodule = PyWin_GetOrLoadLibraryHandle("ktmw32.dll"); + if (hmodule != NULL) { pfnCreateTransaction = (CreateTransactionfunc)GetProcAddress(hmodule, "CreateTransaction"); pfnRollbackTransaction = (RollbackTransactionfunc)GetProcAddress(hmodule, "RollbackTransaction"); pfnRollbackTransactionAsync = (RollbackTransactionAsyncfunc)GetProcAddress(hmodule, "RollbackTransactionAsync"); diff --git a/win32/src/win32tsmodule.cpp b/win32/src/win32tsmodule.cpp index ddd383dee..ac18c03dc 100644 --- a/win32/src/win32tsmodule.cpp +++ b/win32/src/win32tsmodule.cpp @@ -836,10 +836,8 @@ PYWIN_MODULE_INIT_FUNC(win32ts) PyModule_AddIntConstant(module, "NOTIFY_FOR_THIS_SESSION", NOTIFY_FOR_THIS_SESSION); PyModule_AddIntConstant(module, "NOTIFY_FOR_ALL_SESSIONS", NOTIFY_FOR_ALL_SESSIONS); - HMODULE h = GetModuleHandle(L"wtsapi32.dll"); - if (h == NULL) - h = LoadLibrary(L"wtsapi32.dll"); - if (h) { + HMODULE h = PyWin_GetOrLoadLibraryHandle("wtsapi32.dll"); + if (h != NULL) { pfnWTSQueryUserToken = (WTSQueryUserTokenfunc)GetProcAddress(h, "WTSQueryUserToken"); pfnWTSRegisterSessionNotification = (WTSRegisterSessionNotificationfunc)GetProcAddress(h, "WTSRegisterSessionNotification"); @@ -847,10 +845,8 @@ PYWIN_MODULE_INIT_FUNC(win32ts) (WTSUnRegisterSessionNotificationfunc)GetProcAddress(h, "WTSUnRegisterSessionNotification"); } - h = GetModuleHandle(L"kernel32.dll"); - if (h == NULL) - h = LoadLibrary(L"kernel32.dll"); - if (h) { + h = PyWin_GetOrLoadLibraryHandle("kernel32.dll"); + if (h != NULL) { pfnProcessIdToSessionId = (ProcessIdToSessionIdfunc)GetProcAddress(h, "ProcessIdToSessionId"); pfnWTSGetActiveConsoleSessionId = (WTSGetActiveConsoleSessionIdfunc)GetProcAddress(h, "WTSGetActiveConsoleSessionId");