Windows下python用ctypes调用C++程序的动态链接库方法(vs2019)

Linux下我试了没问题的 python3调用cpp的方法——python调用so
Windows下用mingw一直有问题,没找到解决方法。先记vs的。

visual studio

新建动态链接库项目
在这里插入图片描述
或在项目属性里改
在这里插入图片描述
关闭预编译头
在这里插入图片描述
复制粘贴以下代码。代码还是来自上面提到的那篇博客 python3调用cpp的方法——python调用so,针对MSVC稍作修改。vs给的示例程序没用上。

// test.cpp
#include <iostream>

class Test {
private:
    double _calculate(int a, double b);
public:
    double calculate(int a, double b, char c[], int* d, double* e, char** f);
};

double Test::_calculate(int a, double b) {
    double res = a + b;
    std::cout << "res: " << res << std::endl;
    return res;
}

double Test::calculate(int a, double b, char c[], int* d, double* e, char** f) {
    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;
    std::cout << "c: " << c << std::endl;
    std::cout << "d: " << d[0] << d[1] << std::endl;
    std::cout << "e: " << e[0] << e[1] << std::endl;
    std::cout << "f: " << f[0] << f[1] << std::endl;
    return this->_calculate(a, b);
}

extern "C" _declspec(dllexport) Test* test_new()
{
        return new Test;
}
extern "C" _declspec(dllexport) double my_calculate(Test* t, int a, double b, char c[], int* d, double* e, char** f)
{
        return t->calculate(a, b, c, d, e, f);
}

编译时注意选release和x64平台。原因:解决Python调试OSError: [WinError 193] %1 不是有效的 Win32 应用程序。release是因为都生成库了也没必要调试了。将编译后生成的dll文件重命名为test.dll。

# main.py
import ctypes

lib = ctypes.cdll.LoadLibrary('./test.dll')
lib.my_calculate.restype = ctypes.c_double


class Test(object):
    def __init__(self):
        self.obj = lib.test_new()

    def calculate(self, a, b, c, d, e, f):
        res = lib.my_calculate(self.obj, a, b, c, d, e, f)
        return res


def convert_type(data):
    ctypes_map = {int: ctypes.c_int,
                  float: ctypes.c_double,
                  str: ctypes.c_char_p
                  }
    input_type = type(data)
    if input_type is list:
        length = len(data)
        if length == 0:
            print("convert type failed...input is " + data)
            return None
        else:
            arr = (ctypes_map[type(data[0])] * length)()
            for i in range(length):
                arr[i] = bytes(data[i], encoding="utf-8") if (type(data[0]) is str) else data[i]
            return arr
    else:
        if input_type in ctypes_map:
            return ctypes_map[input_type](bytes(data, encoding="utf-8") if type(data) is str else data)
        else:
            print("convert type failed...input is " + data)
            return None


if __name__ == '__main__':
    t = Test()
    A1 = 123
    A2 = 0.789
    A3 = "C789"
    A4 = [456, 789]
    A5 = [0.123, 0.456]
    A6 = ["A123", "B456"]
    print(t.calculate(convert_type(A1), convert_type(A2), convert_type(A3), convert_type(A4), convert_type(A5), convert_type(A6)))

一个最简单的例子

// test.cpp
class Test {
public:
    double mysum(double a, double b) { return a + b; };
};
extern "C" _declspec(dllexport) double my_calculate(double a, double b)
{
    Test t;
    return t.mysum(a, b);
}
# main.py
import ctypes
lib = ctypes.cdll.LoadLibrary('./test.dll')
lib.my_calculate.restype = ctypes.c_double
A1 = 123
A2 = 0.789
print(lib.my_calculate(ctypes.c_double(A1), ctypes.c_double(A2)))

一个结构体的例子

Calling C functions from Python - part 1 - using ctypes

// test.cpp
typedef struct{
    double x, y, z;
}Vector3d;
extern "C" _declspec(dllexport) Vector3d Vec_Add(Vector3d a, Vector3d b)
{
    Vector3d ans;
    ans.x = a.x + b.x;
    ans.y = a.y + b.y;
    ans.z = a.z + b.z;
    return ans;
}
# main.py
import ctypes
lib = ctypes.cdll.LoadLibrary('./test.dll')
class Test(ctypes.Structure):
    _fields_ = [("j1", ctypes.c_double), ("j2", ctypes.c_double), ("j3", ctypes.c_double)]
lib.Vec_Add.restype = Test
vectora = Test(1, 2, 3.5)
vectorb = Test(1.5, 2.5, 4.5)
vectorc = lib.Vec_Add(vectora, vectorb)
print(vectorc.j1, vectorc.j2, vectorc.j3)

参考资料

我看过了没用上但觉得以后可能会用到

C++和python的代码如何相互调用?
浅谈python中使用C/C++:ctypes
How to call C / C++ from Python?
https://docs.python.org/3.11/library/ctypes.html
MingW下动态链接库常识
MinGW gcc 生成动态链接库 dll 的一些问题汇总
Python调用C++ 传数组指针参数
Python使用ctypes模块调用DLL函数传递结构体参数

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值