所以我得到了一个简单的ctypes结构,其中定义了c_void_p字段,但是当我尝试获取它的类型()时,我得到了很长的时间'。奇怪的是,如果变量未在_ fields _中定义,则type()将返回c_void_p。
例如:
import ctypes
from ctypes.wintypes import *
class myClass(ctypes.Structure):
_fields_ = [("x", ctypes.c_void_p)]
def __init__(self):
self.x = ctypes.cast(LPSTR("ABC"), LPVOID)
self.y = ctypes.cast(LPSTR("ABC"), LPVOID)
if __name__ == '__main__':
inst = myClass()
print inst.x, getattr(inst,"x"), type(inst.x)
print inst.y, getattr(inst,"y"), type(inst.y)
这输出以下内容:
38561064 38561064 <type 'long'>
c_void_p(38561064L) c_void_p(38561064L) <class 'ctypes.c_void_p'>
所以基本上,因为变量&#34; x&#34;是_ fields _的一部分,它被转换为&#34; long&#34;,但是&#34; y&#34;没有。
我的问题是,是否可以获得&#34; x&#34;作为c_void_p类型返回??
我知道有些人会说,只做c_void_p(inst.x)。通过这样做,你会丢失一些信息,例如&#34; _objects&#34;其中包含指针指向的对象。对于上面的例子,inst.y._objects是&#34; ABC&#34;但是inst.x._objects不存在,因为inst.x很长,而c_void_p(inst.x)不会带回那些信息
有什么想法吗?