简体   繁体   English

python ctypes:从指针变量获取指向类型

[英]python ctypes: get pointed type from a pointer variable

I have a structure which contains primitive fields (int, uint8, ...) and also pointers. 我有一个结构,其中包含原始字段(int,uint8,...)以及指针。 These pointers often point to an array of different structures type in order to keep a deeply nested structure. 这些指针通常指向不同结构类型的数组,以保持深度嵌套的结构。 For example in C: 例如在C中:

struct A
{
 int field1;
 int field2;
 struct B *fields3;
 unsigned int countofb;
}

struct B
{
  int anotherfield1;
  int anotherfield2;
}

In python with ctypes I create a wrapper of the structures A and B. iterating over _fields_ of structure AI reach the third field field3 and I get a ctype variable of type LP_struct_B . 在具有ctypes的python中,我创建了结构A和B的包装。遍历结构AI的_fields_到达第三字段field3并且得到类型LP_struct_B的ctype变量。

The question is, is there a way, a function, a method of ctypes that convert a pointer to the pointed type ? 问题是,有没有一种方法,一种函数,一种将指针转换为指针类型的ctypes方法?

I need something like 我需要类似的东西

a=A()
st=pointedtype(a.field3) # or also st = pointedtype2(LP_struct_B)
#desired output: st = struct_B

thanks 谢谢

Ok. 好。 I've empirically found the answer. 我凭经验找到了答案。 Simply use the attribute _type_ of the variable or of the pointer type 只需使用变量或指针类型的属性_type_

a=A()
print a.fields3._type_ # struct_B
tipo=type(a.fields3) # tipo=LP_struct_B
print tipo._type_ # struct_B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM