Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last error wrongly set by some modules #2302

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions win32/src/PyWinTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions win32/src/PyWinTypesmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ???
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 7 additions & 17 deletions win32/src/win32apimodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
4 changes: 1 addition & 3 deletions win32/src/win32consolemodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
25 changes: 9 additions & 16 deletions win32/src/win32file.i
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
}
}

%}

Expand Down
66 changes: 30 additions & 36 deletions win32/src/win32gui.i
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
%}

%{
Expand Down
2 changes: 1 addition & 1 deletion win32/src/win32inet.i
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions win32/src/win32job.i
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

%}

Expand Down
4 changes: 1 addition & 3 deletions win32/src/win32net/win32netmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 3 additions & 5 deletions win32/src/win32pipe.i
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
%}

%{
Expand Down
27 changes: 10 additions & 17 deletions win32/src/win32process.i
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions win32/src/win32profilemodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
9 changes: 3 additions & 6 deletions win32/src/win32service.i
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,7 +51,7 @@ EnumServicesStatusExfunc fpEnumServicesStatusEx=NULL;
fp=GetProcAddress(hmod,"EnumServicesStatusExW");
if (fp!=NULL)
fpEnumServicesStatusEx=(EnumServicesStatusExfunc)fp;
}
}
%}

%{
Expand Down
6 changes: 2 additions & 4 deletions win32/src/win32transactionmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading
Loading