Retrieving structs from .so files in Python
Question: I am attempting to write a .so library wrapper for an existing C source code
project, and then call the functions in the .so library from Python. I have
been able to call functions with primitive arguments and return types with no
problem, so I am now working on interfacing with more complex functions that
have arguments that are pointers to structures.

My problem is in creating the structures on the Python side so that I can call
the C-library functions. Some of the structs in the .so library have hundreds
of fields, so I was hoping there was an easier alternative to spelling out all
the fields and types in a Python ctypes `Structure` object.

I would like to be able to write something like this is Python:

    
    
    from ctypes import *
    
    lib = cdll.LoadLibrary("./libexample.so")
    
    class Input(Structure):
        _fields_ = lib.example_struct._fields  ## where `example_struct` is defined in the .so library
                         ## I have no idea if you can actually get the fields of the struct!!
    
    my_input = Input(a,b,c,...)  ## pseudo-code
    my_ptr = pointer(my_input)  ## wrap the input with a pointer
    result = lib.my_lib_func(my_ptr)  ## call .so function with struct
    

This would allow me to easily replicate at least the structure definitions of
the large C structs without having to create and maintain lengthy Python
versions of the struct definitions. Is this possible? Or is there another way
to achieve the same effect?

EDIT: The C source code is third party, so for now, I am looking for an
approach where I don't have to modify the C source.


Answer: The Cython approach is to read and interpret the .h header file. But I do not
say it would be easy.

