本文以实例讲解了Python调用C/C++ DLL动态链接库的方法,具体示例如下:
示例一:
首先,在创建一个DLL工程(本例创建环境为VS 2005),头文件:
02 | #ifdef EXPORT_HELLO_DLL |
03 | #define HELLO_API __declspec(dllexport) |
05 | #define HELLO_API __declspec(dllimport) |
09 | HELLO_API
int IntAdd( int
, int ); |
CPP文件:
2 | #define EXPORT_HELLO_DLL |
4 | HELLO_API int
IntAdd( int
a, int b) |
这里有两个注意点:
(1)弄清楚编译的时候函数的调用约定采用的__cdecl还是__stdcall,因为根据DLL中函数调用约定方式,Python将使用相应的函数加载DLL。
(2)如果采用C++的工程,那么导出的接口需要extern "C",这样python中才能识别导出的函数。
我的工程中采用__cdecl函数调用约定方式进行编译链接产生hello.dll,然后Python中采用ctypes库对hello.dll进行加载和函数调用:
2 | dll = cdll.LoadLibrary( 'hello.dll' ); |
至此,第一个小例子已经完成了,读者可以自己动手尝试一下运行效果。
示例二:
示例一只是一个"hello world"级别的程序,实际运用中更多的需要传递数据结构、字符串等,才能满足我们的需求。那么本示例将展示,如何传递数据结构参数,以及如何通过数据结构获取返回值。
首先编写DLL工程中的头文件:
02 | #ifdef EXPORT_HELLO_DLL |
03 | #define HELLO_API __declspec(dllexport) |
05 | #define HELLO_API __declspec(dllimport) |
08 | #define ARRAY_NUMBER 20 |
16 | int
iArray[ARRAY_NUMBER]; |
22 | HELLO_API
char * GetStructInfo( struct
StructTest* pStruct); |
CPP文件如下:
03 | #define EXPORT_HELLO_DLL |
06 | HELLO_API char * GetStructInfo( struct
StructTest* pStruct) |
08 | for
( int
i = 0; i < ARRAY_NUMBER; i++) |
09 | pStruct->iArray[i] = i; |
10 | pStruct->pChar =
"hello python!" ; |
11 | strcpy
(pStruct->str, "hello world!" ); |
12 | pStruct->number = 100; |
GetStructInfo这个函数通过传递一个StructTest类型的指针,然后对对象中的属性进行赋值,最后返回"just OK".
编写Python调用代码如下,首先在Python中继承Structure构造一个和C DLL中一致的数据结构StructTest,然后设置函数GetStructInfo的参数类型和返回值类型,最后创建一个StructTest对象,并将其转化为指针作为参数,调用函数GetStrcutInfo,最后通过输出数据结构的值来检查是否调用成功:
05 | INTARRAY20 =
c_int *
ARRAY_NUMBER; |
06 | CHARARRAY20 =
c_char *
STR_LEN; |
08 | class StructTest(Structure): |
13 | ( "iArray" , INTARRAY20) |
16 | dll =
cdll.LoadLibrary( 'hello.dll' ); |
17 | GetStructInfo =
dll.GetStructInfo; |
19 | GetStructInfo.restype =
c_char_p; |
21 | GetStructInfo.argtypes =
[POINTER(StructTest)]; |
22 | objectStruct =
StructTest(); |
24 | retStr =
GetStructInfo(byref(objectStruct)); |
26 | print "number: " , objectStruct.number; |
27 | print "pChar: " , objectStruct.pChar; |
28 | print "str: " , objectStruct. str ; |
29 | for i,val
in enumerate (objectStruct.iArray): |
30 | print
'Array[i]: ' , val; |