Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

Access values of nested structure defined in c++ from python code using ctypes

C++ dll returning a pointer to structure as mentioned below but I am not able to access values of struct B and struct C in python ctypes from pointer to struct A.

c++ structure nested classes are like this :

struct A
{
    int age;
    char* name;
    B* ptr_b;
};

struct B
{
    int id;
    C* ptr_c;
};

struct C
{
    char* address;
};

void getData(const A **ptr_a);//dll method to get data

Corresponding ctypes python syntax are :

class A(Structure):
      _fields_ = [(age,   c_uint),
                  (name,  c_wchar_p),
                  (ptr_b, POINTER(B))
                 ]

class B(Structure):
      _fields_ = [(id,   c_uint),
                  (ptr_c, POINTER(C))
                 ]

class C(Structure):
      _fields_ = [(address,   c_wchar_p)
                 ]

I am using a pointer in python to access ctypes structure using:

dll.getData.argtypes = [POINTER(POINTER(A))]
dll.getData.restype = None

ptrptr = POINTER(A)()
dll.getData(byref(ptrptr))

ptr = ptrptr.contents

print(ptr.age) works fine
print(ptr.name) works fine
print(ptr.ptr_b) looks fine because it prints some address
print(ptr.ptr_b.contents.id) fails
print(ptr.ptr_b.contents.ptr_c) fails

Please guide me with the right syntax to get these values.

question from:https://stackoverflow.com/questions/65874298/access-values-of-nested-structure-defined-in-c-from-python-code-using-ctypes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...