Python调用C/C++动态链接库的方法详解

本文通过两个实例展示了Python如何使用ctypes库调用C/C++编写的DLL动态链接库,包括基本类型和复杂数据结构的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文以实例讲解了Python调用C/C++ DLL动态链接库的方法,具体示例如下:

示例一:

首先,在创建一个DLL工程(本例创建环境为VS 2005),头文件:

01//hello.h
02#ifdef EXPORT_HELLO_DLL
03#define HELLO_API __declspec(dllexport)
04#else
05#define HELLO_API __declspec(dllimport)
06#endif
07extern "C"
08{
09 HELLO_API int IntAdd(int , int);
10}

CPP文件:

1//hello.cpp
2#define EXPORT_HELLO_DLL
3#include "hello.h"
4HELLO_API int IntAdd(int a, int b)
5{
6 return a + b;
7}

这里有两个注意点:

(1)弄清楚编译的时候函数的调用约定采用的__cdecl还是__stdcall,因为根据DLL中函数调用约定方式,Python将使用相应的函数加载DLL。

(2)如果采用C++的工程,那么导出的接口需要extern "C",这样python中才能识别导出的函数。

我的工程中采用__cdecl函数调用约定方式进行编译链接产生hello.dll,然后Python中采用ctypes库对hello.dll进行加载和函数调用:

1from ctypes import *
2dll = cdll.LoadLibrary('hello.dll');
3ret = dll.IntAdd(2, 4);
4print ret;

至此,第一个小例子已经完成了,读者可以自己动手尝试一下运行效果。

示例二:

示例一只是一个"hello world"级别的程序,实际运用中更多的需要传递数据结构、字符串等,才能满足我们的需求。那么本示例将展示,如何传递数据结构参数,以及如何通过数据结构获取返回值。

首先编写DLL工程中的头文件:

01//hello.h
02#ifdef EXPORT_HELLO_DLL
03#define HELLO_API __declspec(dllexport)
04#else
05#define HELLO_API __declspec(dllimport)
06#endif
07 
08#define ARRAY_NUMBER 20
09#define STR_LEN 20
10 
11struct StructTest
12{
13 int number;
14 char* pChar;
15 char str[STR_LEN];
16 int iArray[ARRAY_NUMBER];
17};
18 
19extern "C"
20{
21 //HELLO_API int IntAdd(int , int);
22 HELLO_API char* GetStructInfo(struct StructTest* pStruct);
23}

CPP文件如下:

01//hello.cpp
02#include <string.h>
03#define EXPORT_HELLO_DLL
04#include "hello.h"
05 
06HELLO_API char* GetStructInfo(struct StructTest* pStruct)
07{
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;
13 return "just OK";
14}

GetStructInfo这个函数通过传递一个StructTest类型的指针,然后对对象中的属性进行赋值,最后返回"just OK".

编写Python调用代码如下,首先在Python中继承Structure构造一个和C DLL中一致的数据结构StructTest,然后设置函数GetStructInfo的参数类型和返回值类型,最后创建一个StructTest对象,并将其转化为指针作为参数,调用函数GetStrcutInfo,最后通过输出数据结构的值来检查是否调用成功

01from ctypes import *
02ARRAY_NUMBER = 20;
03STR_LEN = 20;
04#define type
05INTARRAY20 = c_int * ARRAY_NUMBER;
06CHARARRAY20 = c_char * STR_LEN;
07#define struct
08class StructTest(Structure):
09  _fields_ = [
10    ("number", c_int),
11    ("pChar", c_char_p),
12    ("str", CHARARRAY20),
13    ("iArray", INTARRAY20)
14        ]
15#load dll and get the function object
16dll = cdll.LoadLibrary('hello.dll');
17GetStructInfo = dll.GetStructInfo;
18#set the return type
19GetStructInfo.restype = c_char_p;
20#set the argtypes
21GetStructInfo.argtypes = [POINTER(StructTest)];
22objectStruct = StructTest();
23#invoke api GetStructInfo
24retStr = GetStructInfo(byref(objectStruct));
25#check result
26print "number: ", objectStruct.number;
27print "pChar: ", objectStruct.pChar;
28print "str: ", objectStruct.str;
29for i,val in enumerate(objectStruct.iArray):
30  print 'Array[i]: ', val;
31print retStr;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值