基本概念
CDLL:用于加载标准C的动态链接库(DLL)。
argtypes:定义函数参数类型的元组。
restype:定义函数返回值类型。
1. 简单函数调用
假设我们有一个简单的C++ DLL,包含一个函数add,用来计算两个整数的和。
C++ 代码
// mylib.cpp
extern "C" __declspec(dllexport) int add(int a, int b) {
return a + b;
}
编译生成mylib.dll。
Python 代码
import ctypes
# 加载DLL
mylib = ctypes.CDLL('mylib.dll')
# 定义函数原型
mylib.add.argtypes = (ctypes.c_int, ctypes.c_int)
mylib.add.restype = ctypes.c_int
# 调用函数
result = mylib.add(5, 3)
print(f"Result of add(5, 3): {result}")
2. 函数返回字符串
假设我们有一个C++函数返回一个字符串。
C++ 代码
// mylib.cpp
extern "C" __declspec(dllexport) const char* greet() {
return "Hello from C++!";
}
编译生成mylib.dll。
Python 代码
import ctypes
# 加载DLL
mylib = ctypes.CDLL('mylib.dll')
# 定义函数原型
mylib.greet.restype = ctypes.c_char_p
# 调用函数
message = mylib.greet()
print(f"Greet message: {message.decode('utf-8')}")
3. 使用指针
假设我们有一个C++函数使用指针来修改传入的值。
C++ 代码
// mylib.cpp
extern "C" __declspec(dllexport) void increment(int* value) {
(*value)++;
}
编译生成mylib.dll。
Python 代码
import ctypes
# 加载DLL
mylib = ctypes.CDLL('mylib.dll')
# 定义函数原型
mylib.increment.argtypes = (ctypes.POINTER(ctypes.c_int),)
mylib.increment.restype = None
# 创建一个整数值并传递指针
value = ctypes.c_int(10)
print(f"Before increment: {value.value}")
mylib.increment(ctypes.byref(value))
print(f"After increment: {value.value}")
4. 结构体作为参数
假设我们有一个C++函数使用结构体作为参数。
C++ 代码
// mylib.cpp
struct Point {
int x;
int y;
};
extern "C" __declspec(dllexport) void set_point(Point* point, int x, int y) {
point->x = x;
point->y = y;
}
编译生成mylib.dll。
Python 代码
import ctypes
# 定义结构体
class Point(ctypes.Structure):
_fields_ = [("x", ctypes.c_int),
("y", ctypes.c_int)]
# 加载DLL
mylib = ctypes.CDLL('mylib.dll')
# 定义函数原型
mylib.set_point.argtypes = (ctypes.POINTER(Point), ctypes.c_int, ctypes.c_int)
mylib.set_point.restype = None
# 创建结构体实例
point = Point()
mylib.set_point(ctypes.byref(point), 5, 10)
print(f"Point coordinates: ({point.x}, {point.y})")
5. 函数抛出异常
假设我们有一个C++函数可能抛出异常。
C++ 代码
// mylib.cpp
#include <stdexcept>
extern "C" __declspec(dllexport) void may_throw(int condition) {
if (condition == 0) {
throw std::runtime_error("Condition is zero");
}
}
编译生成mylib.dll。
Python 代码
import ctypes
# 加载DLL
mylib = ctypes.CDLL('mylib.dll')
# 定义函数原型
mylib.may_throw.argtypes = (ctypes.c_int,)
mylib.may_throw.restype = None
# 调用函数并捕获异常
try:
mylib.may_throw(0)
except OSError as e:
print(f"Caught an exception: {e}")
总结
通过以上示例,我们展示了如何使用Python的ctypes库调用C++的DLL接口。无论是简单的函数调用、返回字符串、使用指针、结构体参数还是处理异常,ctypes都能提供灵活的方式与C++代码进行交互。