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

2020-11-10 Thread Eryk Sun


Eryk Sun  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-11-10 Thread Taekab Park


New submission from Taekab Park :

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.

result
actual void* : 0x7FFDA4322018  <-> python void* : 0xA4322018

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

== python == 
import ctypes

print(ctypes.sizeof(ctypes.c_void_p))
_testdll = ctypes.CDLL(r"TestDll.dll")

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

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

result = _testdll.GetPointer1()
print(tohex(result, 64))
_testdll.SetPointer1(result)

--
components: ctypes
files: example.zip
messages: 380676
nosy: sh4dow
priority: normal
severity: normal
status: open
title: It seems like ctypes code makes truncated pointer values in x64(access 
violations)
type: crash
versions: Python 3.6, Python 3.9
Added file: https://bugs.python.org/file49589/example.zip

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com