Skip to content

Commit

Permalink
feat: propagate PyWin_GetOrLoadLibraryHandle to other modules (initia…
Browse files Browse the repository at this point in the history
…lization)
  • Loading branch information
CristiFati committed Oct 24, 2024
1 parent 6fc5962 commit b92e2d9
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 81 deletions.
6 changes: 2 additions & 4 deletions win32/src/PyWinTypesmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,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
9 changes: 5 additions & 4 deletions win32/src/win32apimodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4772,13 +4772,14 @@ 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 { return Py_BuildValue("i", result); }
else {
return Py_BuildValue("i", result);
}
}

// @pymethod |win32api|SetThreadLocale|Sets the current thread's locale.
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
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
12 changes: 4 additions & 8 deletions win32/src/win32tsmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,21 +836,17 @@ 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");
pfnWTSUnRegisterSessionNotification =
(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");
Expand Down

0 comments on commit b92e2d9

Please sign in to comment.