Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ctypes:从任意整数构造指针_Python_Ctypes_Pywin32 - Fatal编程技术网

Python ctypes:从任意整数构造指针

Python ctypes:从任意整数构造指针,python,ctypes,pywin32,Python,Ctypes,Pywin32,出于低级目的,我需要从给定为整数的任意地址构造ctypes指针。例如: INTP = ctypes.POINTER(ctypes.c_int) p = INTP(0x12345678) # i *know* this is the address 但所有这些尝试都会导致失败 TypeError: expected c_long instead of int 我能做些什么来克服这个问题吗?如果有人想知道我为什么需要它,那么这样做是为了从win32file.PyOVERLAPPED中提取OVER

出于低级目的,我需要从给定为整数的任意地址构造ctypes指针。例如:

INTP = ctypes.POINTER(ctypes.c_int)
p = INTP(0x12345678) # i *know* this is the address
但所有这些尝试都会导致失败

TypeError: expected c_long instead of int
我能做些什么来克服这个问题吗?如果有人想知道我为什么需要它,那么这样做是为了从
win32file.PyOVERLAPPED
中提取
OVERLAPPED
结构,以便将ctypes公开的函数与win32file包装的API集成

谢谢,

-Tomer

您可以使用
ctypes.cast(addr,type)
。我将扩展您的示例,通过已知对象获取地址,以演示:

INTP = ctypes.POINTER(ctypes.c_int)
num = ctypes.c_int(42)
addr = ctypes.addressof(num)
print 'address:', addr, type(addr)
ptr = ctypes.cast(addr, INTP)
print 'pointer:', ptr
print 'value:', ptr[0]
输出:

address: 4301122528 <type 'int'>
pointer: <__main__.LP_c_int object at 0x1005decb0>
value: 42
地址:4301122528
指针:
价值:42

@sebulba:如果是,请将答案标记为正确:)打印('value:',ptr.contents)怎么样