通过Ctypes从C到Python-将函数指针的结构包装为静态函数 [英] C to Python via Ctypes - Wrapping Struct of Function Pointers to Static Functions
问题描述
我在C库中有这样的结构。
DataFn中的函数指针指向静态函数。
I have structs in a C library that are like this. The function pointers in DataFn point to static functions.
.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。
Using objdump, the table only contains DATAFUNC and a few other things from gcc.
这在C语言中很好,在其中调用fn1就像DATAFUNC.fn1(...,...),但是如何将其包装这样就可以在python w / ctypes中调用fn1了吗?
This is fine in C where calling fn1 would go like DATAFUNC.fn1(..., ...), but how would something like this be wrapped around so fn1 can be called in python w/ ctypes?
示例python
libc = ctypes.cdll.LoadLibrary("./data.so")
print(libc.DATAFUNC)
导致
< _FuncPtr对象位于0x6ffffcd7430>
< a href = https://stackoverflow.com/questions/3854388/interacting-with-ac-struct- contains-only-function-pointers-using-ctypes-in-py>与此类似,但是没有
推荐答案
[Python 3.Docs]:ctypes-Python的外部函数库包含eve
我相信缺少的主要部分是 ctypes 的 in_dll 方法>类型(访问从dll导出的值部分)。
I believe that the main piece missing, was the in_dll method of a ctypes type (Accessing values exported from dll section).
除此之外,为了与 C 一起使用数据,您需要让 Python 知道数据格式。这适用于:
Other than that, in order to work with C data, you need to let Python know of the data format. That applies to:
- struct s。通过子类化
ctypes定义 Python 对应对象。结构
- 函数指针(适用于您的情况)。使用
ctypes定义它们。CFUNCTYPE
- structs. Define Python counterparts by subclassing
ctypes.Structure
- Function pointers (applies to your case). Define them using
ctypes.CFUNCTYPE
我准备了一个简化的示例来说明以上。请注意,为了简化操作,我没有进行任何错误处理(检查 NULL (应该是您应该))。
I prepared a simplified example that illustrates the above. Note that I didn't do any error handling (checking for NULLs (which you should)), to keep things simple.
ch :
struct Data {
int i;
};
typedef struct {
int (* const fn1) (struct Data*, const char*);
} DataFn;
extern DataFn const DATAFUNC;
cc :
#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
这篇关于通过Ctypes从C到Python-将函数指针的结构包装为静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!