Python And Ctypes - Magnification Dll
Solution 1:
I want to start by pointing out other stuff that's not right in the code (not necessarily related to the error):
Seems like reinventing the wheel here. Use the wintypes module (only mentioned in the documentation), it has everything needed for this scenario:
>>> from ctypes import wintypes >>> dir(wintypes) ['ATOM', 'BOOL', 'BOOLEAN', 'BYTE', 'CHAR', 'COLORREF', 'DOUBLE', 'DWORD', 'FILETIME', 'FLOAT', 'HACCEL', 'HANDLE', 'HBITMAP', 'HBRUSH', 'HCOLORSPACE', 'HDC', 'HDESK', 'HDWP', 'HENHMETAFILE', 'HFONT', 'HGDIOBJ', 'HGLOBAL', 'HHOOK', 'HICON', 'HINSTANCE', 'HKEY', 'HKL', 'HLOCAL', 'HMENU', 'HMETAFILE', 'HMODULE', 'HMONITOR', 'HPALETTE', 'HPEN', 'HRGN', 'HRSRC', 'HSTR', 'HTASK', 'HWINSTA', 'HWND', 'INT', 'LANGID', 'LARGE_INTEGER', 'LCID', 'LCTYPE', 'LGRPID', 'LONG', 'LPARAM', 'LPBOOL', 'LPBYTE', 'LPCOLESTR', 'LPCOLORREF', 'LPCSTR', 'LPCVOID', 'LPCWSTR', 'LPDWORD', 'LPFILETIME', 'LPHANDLE', 'LPHKL', 'LPINT', 'LPLONG', 'LPMSG', 'LPOLESTR', 'LPPOINT', 'LPRECT', 'LPRECTL', 'LPSC_HANDLE', 'LPSIZE', 'LPSIZEL', 'LPSTR', 'LPUINT', 'LPVOID', 'LPWIN32_FIND_DATAA', 'LPWIN32_FIND_DATAW', 'LPWORD', 'LPWSTR', 'MAX_PATH', 'MSG', 'OLESTR', 'PBOOL', 'PBOOLEAN', 'PBYTE', 'PCHAR', 'PDWORD', 'PFILETIME', 'PFLOAT', 'PHANDLE', 'PHKEY', 'PINT', 'PLARGE_INTEGER', 'PLCID', 'PLONG', 'PMSG', 'POINT', 'POINTL', 'PPOINT', 'PPOINTL', 'PRECT', 'PRECTL', 'PSHORT', 'PSIZE', 'PSIZEL', 'PSMALL_RECT', 'PUINT', 'PULARGE_INTEGER', 'PULONG', 'PUSHORT', 'PWCHAR', 'PWIN32_FIND_DATAA', 'PWIN32_FIND_DATAW', 'PWORD', 'RECT', 'RECTL', 'RGB', 'SC_HANDLE', 'SERVICE_STATUS_HANDLE', 'SHORT', 'SIZE', 'SIZEL', 'SMALL_RECT', 'UINT', 'ULARGE_INTEGER', 'ULONG', 'USHORT', 'VARIANT_BOOL', 'WCHAR', 'WIN32_FIND_DATAA', 'WIN32_FIND_DATAW', 'WORD', 'WPARAM', '_COORD', '_FILETIME', '_LARGE_INTEGER', '_POINTL', '_RECTL', '_SMALL_RECT', '_ULARGE_INTEGER', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ctypes', 'tagMSG', 'tagPOINT', 'tagRECT', 'tagSIZE']
As a subnote,
BOOL
's custom definition doesn't match Win's one:>>> ctypes.sizeof(ctypes.c_bool) 1>>> ctypes.sizeof(wintypes.BOOL) 4
which is a candidate for Access violation
- Stuff like
self.LPRECT = LPRECT = ctypes.POINTER(RECT)
just looks odd. Types shouldn't be members of an instance - Try to follow [Python]: PEP 8 -- Style Guide for Python Code
- Other small issues that don't worth being mentioned
Going back to the error: it's ERROR_INVALID_PARAMETER, because all 3 arguments passed to MagGetInputTransform are NULL pointers, as [Python 3]: Pointers states:
Calling the pointer type without an argument creates a
NULL
pointer.
To fix it (this is just one way - which I find the most straightforward) change:
The 3 variables initialization:
fInputTransformEnabled = wintypes.BOOL() rcInputTransformSource = wintypes.RECT() rcInputTransformDest = wintypes.RECT()
The way they are passed to MagGetInputTransform:
if self.mag.MagGetInputTransform(ctypes.byref(fInputTransformEnabled), ctypes.byref(rcInputTransformSource), ctypes.byref(rcInputTransformDest)):
and everything should be fine (however all 3 variables have been populated with 0s).
NB: When you encounter an error on Win, [MSDN]: GetLastError function is your best friend!
Post a Comment for "Python And Ctypes - Magnification Dll"