【问题标题】:C to Python via Ctypes - Wrapping Struct of Function Pointers to Static FunctionsC to Python via Ctypes - 将函数指针结构包装到静态函数
【发布时间】:2018-10-02 09:19:19
【问题描述】:

我在 C 库中有这样的结构。 DataFn 中的函数指针指向静态函数。

.h

struct Data {
    int i;
    int *array;
};

typedef struct {
    bool (* const fn1) (struct Data*, const char *source);
    ....
} DataFn;
extern DataFn const DATAFUNC

使用 objdump,该表仅包含 DATAFUNC 和 gcc 中的一些其他内容。

这在 C 中很好,调用 fn1 会像 DATAFUNC.fn1(..., ...),但是如何将这样的东西包裹起来,以便可以在带有 ctypes 的 python 中调用 fn1?

示例python

libc = ctypes.cdll.LoadLibrary("./data.so")
print(libc.DATAFUNC)

导致 <_FuncPtr object at 0x6ffffcd7430>

This is similar, but there isn't a factory function.

【问题讨论】:

    标签: python c struct function-pointers ctypes


    【解决方案1】:

    [Python 3.Docs]: ctypes - A foreign function library for Python 包含解决此问题所需的一切。

    我认为缺少的主要部分是 ctypes 类型的 in_dll 方法(访问从 dll 导出的值部分)。

    除此之外,为了使用 C 数据,您需要让 Python 知道数据格式。这适用于:

    • 结构。通过子类化 ctypes.Structure 定义 Python 对应项
    • 函数指针(适用于您的情况)。使用ctypes.CFUNCTYPE 定义它们

    我准备了一个简化的示例来说明上述内容。请注意,为了简单起见,我没有进行任何错误处理(检查 NULL您应该这样做))。 p>

    c.h

    struct Data {
        int i;
    };
    
    
    typedef struct {
        int (* const fn1) (struct Data*, const char*);
    } DataFn;
    
    
    extern DataFn const DATAFUNC;
    

    c.c

    #include <stdio.h>
    #include "c.h"
    
    
    static int func1(struct Data *pData, const char *source) {
        printf("From C - Data.i: [%d], source: [%s]\n", pData->i, source);
        return -255;
    }
    
    
    DataFn const DATAFUNC = {&func1};
    

    code00.py

    #!/usr/bin/env python3
    
    
    import sys
    from ctypes import c_int, c_char_p, Structure, CDLL, CFUNCTYPE, POINTER, byref
    
    
    class Data(Structure):
        _fields_ = [
            ("i", c_int),
        ]
    
    
    fn1_type = CFUNCTYPE(c_int, POINTER(Data), c_char_p)
    
    
    class DataFn(Structure):
        _fields_ = [
            ("fn1", fn1_type),
        ]
    
    
    def main():
        data = Data(127)
        dll = CDLL("./c.so")
        data_func = DataFn.in_dll(dll, "DATAFUNC")
        ret = data_func.fn1(byref(data), "abcd".encode())
        print("DATAFUNC.fn1 returned {:d}".format(ret))
    
    
    if __name__ == "__main__":
        print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
        main()
    

    输出

    [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049962265]> ls
    c.c  c.h  code00.py
    [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049962265]> gcc -shared -fPIC -o c.so c.c
    [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049962265]> ls
    c.c  c.h  code.py  c.so
    [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049962265]> objdump -t c.so | grep DATAFUNC
    0000000000200e10 g     O .data.rel.ro   0000000000000008              DATAFUNC
    [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049962265]> python3 code00.py
    Python 3.5.2 (default, Nov 23 2017, 16:37:01)
    [GCC 5.4.0 20160609] on linux
    
    From C - Data.i: [127], source: [abcd]
    DATAFUNC.fn1 returned -255
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多