相关疑难解决方法(0)

通过自省从 ctype 结构中获取元素?

我找不到任何可以帮助我解决此类问题的方法:我正在尝试获取作为嵌套结构一部分的属性的偏移量,例如:

数据类型.py

class FirstStructure (ctypes.Structure):
    _fields_ = [('Junk', ctypes.c_bool),
                ('ThisOneIWantToGet', ctypes.c_int8)
                ]


class SecondStructure (ctypes.Structure):
    _fields_ = [('Junk', ctypes.c_double),
                ('Example', FirstStructure)
                ]
Run Code Online (Sandbox Code Playgroud)

重要的是,我只知道父结构的名称,SecondStructure我完全不知道那里可以有多少嵌套结构。

我想在这里做的是ThisOneIWantToGetSecondStructure.

我知道有一种ctypes.adressof方法适用于 ctypes 对象。有没有什么简单的方法来获取嵌套参数的对象,所以我可以做这样的事情:

do_something.py

import data_types as dt
par_struct_obj = getattr(dt, 'SecondStructure')
par_obj = getattr(par_struct_obj , 'ThisOneIWantToGet')
print ctypes.addressof(parameter) - ctypes.addressof(parent_structure)
Run Code Online (Sandbox Code Playgroud)

python ctypes

5
推荐指数
1
解决办法
2049
查看次数

标签 统计

ctypes ×1

python ×1