[issue42311] It seems like ctypes code makes truncated pointer values in x64(access violations)

Eryk Sun report at bugs.python.org
Tue Nov 10 11:46:38 EST 2020


Eryk Sun <eryksun at gmail.com> added the comment:

A function that returns a pointer needs an explicit `restype` set. A function parameter that's a pointer generally requires `argtypes` to be set. For example:

    _testdll.GetPointer.restype = ctypes.c_void_p
    _testdll.SetPointer.argtypes = (ctypes.c_void_p,)

Unfortunately the ctypes documentation starts with a tutorial that promotes bad practices and misuses WinAPI GetModuleHandle multiple times, which gives people the wrong idea about pointer return values. However, the tutorial does mention that Python integers are passed as C int values by default and that C int is the default return type. It also shows how to use the `argtypes` and `restype` attributes.

----------
nosy: +eryksun
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42311>
_______________________________________


More information about the Python-bugs-list mailing list