This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: It seems like ctypes code makes truncated pointer values in x64(access violations)
Type: crash Stage: resolved
Components: ctypes Versions: Python 3.9, Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, sh4dow
Priority: normal Keywords:

Created on 2020-11-10 15:17 by sh4dow, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit sh4dow, 2020-11-10 15:17 example code c++, python
Messages (2)
msg380676 - (view) Author: Taekab Park (sh4dow) Date: 2020-11-10 15:17
It seems like ctypes code makes truncated pointer values in x64(access violations)

windows10 x64
options : __cdecl(/Gd)
tested : python(x64) 3.2, 3.4, 3.6, 3.9 
dll build with vc++2015 

The test code is simple.
Simply loaded TestDll and called function and checked return void* value.

actual void* : 0x00007FFDA4322018  <-> python void* : 0xFFFFFFFFA4322018

== C code ==
void* GetPointer();
SetPointer(void* value);
INT64 GetPointer1();
void SetPointer1(INT64 obj);

== python == 
import ctypes

_testdll = ctypes.CDLL(r"TestDll.dll")

def tohex(val, nbits):
	return hex((val + (1 << nbits)) % (1 << nbits))

result = _testdll.GetPointer()
print(tohex(result, 64))

result = _testdll.GetPointer1()
print(tohex(result, 64))
msg380688 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-11-10 16:46
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.
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86477
2020-11-10 16:46:38eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg380688

resolution: not a bug
stage: resolved
2020-11-10 15:17:16sh4dowcreate