Skip to content

Commit

Permalink
fix: turn macro into function
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiFati committed Oct 23, 2024
1 parent 8ec6ab4 commit 2f5824c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
18 changes: 2 additions & 16 deletions win32/src/PyWinTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 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_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 ???
Expand Down
21 changes: 12 additions & 9 deletions win32/src/win32apimodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions win32/src/win32gui.i
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,19 @@ 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");
pfnAnimateWindow = (AnimateWindowfunc)GetProcAddress(hmodule,"AnimateWindow");
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");
Expand All @@ -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
}
%}

%{
Expand Down

0 comments on commit 2f5824c

Please sign in to comment.