ctypes是Python的一个外部库,提供和C语言兼容的数据类型,可以很方便地调用C DLL中的函数。ctypes的官方文档在https://docs.python.org/3/library/ctypes.html。
1. ctypes基本数据类型映射表
参数类型预先设定好,或者在调用函数时再把参数转成相应的c_***类型。ctypes的类型对应如下:
ctypes type | C type | Python Type |
c_char | char | 1-character string |
c_wchar | wchar_t | 1-character unicode string |
c_byte | char | int/long |
c_ubyte | unsigned char | int/long |
c_bool | bool | bool |
c_short | short | int/long |
c_ushort | unsigned short | int/long |
c_int | int | int/long |
c_uint | unsigned int | int/long |
c_long | long | int/long |
c_ulong | unsigned long | int/long |
c_longlong | __int64 or longlong | int/long |
c_ulonglong | unsigned __int64 or unsigned long long | int/long |
c_float | float | float |
c_double | double | float |
c_longdouble | long double float | float |
c_char_p | char * | string or None |
c_wchar_p | wchar_t * | unicode or None |
c_void_p | void * | int/long or None |
对应的指针类型是在后面加上"_p",如int*是c_int_p等等。在python中要实现c语言中的结构,需要用到类。
2. 加载DLL
访问dll,首先需引入ctypes库
from ctypes import *
假设你已经有了一个的DLL(名字是add.dll),且该DLL有一个符合cdecl(这里强调调用约定是因为,stdcall调用约定和cdecl调用约定声明的导出函数,在使用python加载时使用的加载函数是不同的,后面会有说明)调用约定的导出函数Add。
stdcall调用约定:两种加载方式
cdecl调用约定:也有两种加载方式
其实windll和cdll分别是WinDLL类和CDll类的对象。
3. 调用DLL方法
加载dll后会返回一个DLL对象,使用其中的函数方法则相当于操作该对象的对应属性。
注意,经过stdcall声明的方法,如果不是用def文件声明的导出函数或者extern “C” 声明的话,编译器会对函数名进行修改
函数参数申明,通过设置函数的argtypes属性
函数返回类型,函数默认返回c_int类型,如果需要返回其他类型,需要设置函数的restype属性
如下:
test.c(动态库源代码)
编译:gcc -g -fPIC -shared -o libtest.so test.c
最后运行结果:
4. 指针与引用
常用的通过调用ctypes类型的指针函数来创建指针实例:
对指针实例赋值只会改变其指向的内存地址,而不是改变内存的内容,与其他语言类似,如需要可改变内容的字符串,可须使用create_string_buffer()
不带参数的调用指针类型创建一个NULL指针, NULL指针有一个False布尔值
指针实例有一个contents属性,返回这个指针所指向的对象。
另外,byref()是用来传递引用参数,pointer()作为传参通常会创建一个实际的指针对象,当不需要实际指针对象时,则可使用byref()
byref(n)返回的相当于C的指针右值&n,本身没有被分配空间:
>>> from ctypes import *
>>> n = c_int(0)
>>> p = byref(n)
>>> pp = byref(p)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
pp = byref(p)
TypeError: byref() argument must be a ctypes instance, not 'CArgObject'
pointer返回的相当于指针左值T* p=&n,可以改变,可以取地址:
>>> from ctypes import *
>>> n = c_int(0)
>>> q = pointer(n)
>>> q.contents = c_int(1)
>>> qq = byref(q)
>>> dir(qq)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_obj']
>>> type(qq)
<class 'CArgObject'> #'CArgObject'是什么对象???
>>> qq
<cparam 'P' (010AD238)>
>>> q
<__main__.LP_c_long object at 0x010AD210>
>>> q.contents
c_long(1)
对于T**参数,通常你得构造一个pointer,然后byref传进去
5. 结构体类型处理
Structures和Unions必须继承Structure和Union基础类,它们都在ctypes模块中定义,每一个子类必须定义个_fields_属性,_fields_是一个二维的tuples列表,包含着每个field的name及type,这field类型必须是一个ctypes类型,如c_int,或者任何其他的继承ctypes的类型,如Structure, Union, Array, 指针等。
例如有一个简单结构,包含两个整型x和y,可如下初始化一个结构:
另外,如结构体用于链表操作,即包含指向结构体指针时,若直接定义:
则python会报错type未定义,:
6、回调函数:
1. 定义回调函数类型,类似C中的函数指针,如void ( * event_cb )( void * data, void * user_data ),定义为:
event_cb = CFUNCTYPE( None, c_void_p, c_void_p )
None表示返回值是void,也可以为其它类型,剩余俩个参数与C中的回调参数一致。
2. 定义Python回调函数:
def event_arrived( data, user_data ):
#parsing data
#...
#return something or None
3. 注册回调函数:
register_events_listener( event_cb( event_arrived ), c_void_p( 0 ))
7、传递结构体指针:
1. 定义结构体:
class MyStructure( Structure )
_fields_ = [
( 'x', c_int ),
( 'y', c_int )]
2. 传递结构体:
_local = MyStructure( 1, 2 )
process_this_structure( byref( _local ))
8、函数参数为char**或者数组指针
对应c函数为fun(char ** ppdata, int*idlist)
python对应的参数设置:fun.py
patterns=["qwerqwer", "12eweqwe", "asdfdgfgg"]
ids= [1,2,3,4,5]
a_patterns = (c_char_p * len(patterns))()
a_patterns[:] = patterns
ids = (c_uint * len(ids))(*ids)
lib = ctypes.cdll.LoadLibrary(libpath)
lib.fun.argtypes = [(c_char_p*len(patterns)), POINTER(c_uint)]
lib.fun(a_patterns,ids)
持续更新。。。