python使用ctypes调用C编译dll函数方法
1 生成被调用的C dll
1.1 C文件编译为C动态链接库
- 在windows下,需要配置visual studio的工程设置,将工程编译为dll,细节不在这里赘述
- gcc环境下,需要编译为.so文件,需要修改makefile的链接参数,这里也不再赘述
1.2 用于外部引用的C函数声明
1.2.1 声明用于作为dll符号给外部调用
在函数声明加入前缀,如
__declspec(dllexport) int Fun(int a, int b)
否则在加载该dll时会提示找不到该符号
在windows下可以通过vs自带的dumpbin工具查看可被调用符号
dumpbin /exports test.dll
1.2.2 C函数的调用规定
C函数在调用过程中关于参数传递和压栈由多种规定,作为dll提供给其他程序调用时,必须明确并统一为同一种调用规定,否则会导致栈破坏,编译器负责具体实现调用规定,主要有以下几种调用规定
调用规定 | 声明 | 编译符号修饰 | 调用规则 | 说明 |
---|---|---|---|---|
_stdcall | __declspec(dllexport) int __stdcall fun(int a, int b) |
_fun@number | 参数从右向左入栈,调用者压栈,被调者负责弹栈 | win32 API默认调用规则 |
_cdecl | __declspec(dllexport)int __cdecl fun(int a, int b) |
_fun | 参数从右向左入栈,调用者负责压栈和弹栈 | C/C++默认调用规则 |
_fastcall | __declspec(dllexport)int __fastcall fun(int a, int b) |
@fun@number | 寄存器和栈共同参数与参数传递 | 寄存器传参提高性能,难以跨平台 |
2 ctypes加载dll库接口
python下调用C库有多种方式,ctypes是其中一种比较方便的,调用时首先需要加载dll文件,根据C dll的调用规定不同需要使用不同接口,使用ctypes需要import ctypes
库
- 对于stdcall的C dll
import ctypes Objdll = ctypes.windll.LoadLibrary("dllpath") #接口1 Objdll = ctypes.WinDLL("dllpath") #接口2
以上两种接口都是可用的
- 对于cdecl的C dll
import ctypes Objdll = ctypes.cdll.LoadLibrary("dllpath") Objdll = ctypes.CDLL("dllpath")
对于简单的C函数,例如int add(int a, int b)
, 此时就可以直接调用了,如
import ctypes Objdll = ctypes.cdll.LoadLibrary("dllpath") Objdll = ctypes.CDLL("dllpath") c = Objdll.all(1,3) print(c) # 4
3 ctypes调用C函数参数传递
对于较复杂的C函数的参数情况,ctypes调用时对入参和出餐做一定处理,这里分情况讨论
3.1 出参为指针
- C代码
/* Divide two numbers */ int divide(int a, int b, int *remainder) { int quot = a / b; *remainder = a % b; return quot; }
- python代码
# int divide(int, int, int *) _divide = _mod.divide_divide.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int))_divide.restype = ctypes.c_int def divide(x, y): rem = ctypes.c_int() quot = _divide(x, y, rem) return quot,rem.value
3.2 入参为字符串
- C代码
void PrintfInfo(char *str) { printf("the str is %s\n", str); }
- python代码
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb' pStr = ctypes.c_char_p( ) pStr.value = sBuf.decode() #c_char_p类型的value只接受bytes类型数据 dll.PrintInfo(pStr)
这里使用了ctypes的内置类型c_char_p,对应于C的char数组,更多ctypes类型与C类型对照在后面附上
3.4 入/出参为结构体
- C代码
typedef struct { char words[10]; }keywords; typedef struct { keywords *kws; unsigned int len; }outStruct; extern "C"int __declspec(dllexport) test(outStruct *o); int test(outStruct *o) { unsigned int i = 4; o->kws = (keywords *)malloc(sizeof(unsigned char) * 10 * i); strcpy(o->kws[0].words, "The First Data"); strcpy(o->kws[1].words, "The Second Data"); o->len = i; return 1; }
- python代码
# C结构体需要专门定义class对应 class keywords(Structure): _fields_ = [('words', c_char *10),] class outStruct(Structure): _fields_ = [('kws', POINTER(keywords)), ('len', c_int),] o = outStruct() dll.test(byref(o)) print o.kws[0].words; print o.kws[1].words; print o.len
以上包含了几种主要的参数传递情况,ctypes也提供了一个较为完整的python类型和C类型的对照,如下:
image.png
原文地址:https://www.jianshu.com/p/b338e55c3b71
相关推荐
-
用KNN来进行验证码识别 python基础
2018-2-25
-
(上)python3 selenium3 从框架实现学习selenium让你事半功倍 python基础
2020-6-11
-
python发布webservice接口 python基础
2019-8-26
-
python的模块引用和查找路径 python基础
2019-8-15
-
Python 的 enum 模块源码分析 python基础
2018-12-11
-
Python之父重回决策层,社区未来如何发展? python基础
2020-5-31
-
快速教你如何搭建数据驱动自动化测试框架? python基础
2019-6-30
-
关于Python列表中10个最常见的问题 python基础
2019-9-11
-
python异常处理 python基础
2019-3-20
-
python定时任务APScheduler python基础
2019-9-7