Pythonctypes.结构“c_void_p”类型转换为“long”

2024-07-05 11:52:47 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个简单的ctypes结构,它定义了一个c\u void_p字段,但是当我试图获得它的type()时,我得到的是'long'。奇怪的是,如果变量没有在字段中定义,那么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)

这将输出以下内容:

^{pr2}$

所以基本上,由于变量“x”是∗字段的一部分,所以它被转换成“long”,但“y”不是。在

我的问题是,有没有可能把“x”作为c_void_p类型返回??在

我知道你们中的一些人会说,就这样做吧。这样做会丢失一些信息,例如包含指针指向的对象的“\u objects”的内容。对于上面的示例,inst.y.\u objects是“ABC”,但是inst.x.\u对象不存在,因为inst.x是long,并且c_covoid_p(inst.x)不会返回该信息

有什么想法吗??在


Tags: importself定义typemyclassctypeslongabc