成都市建委电话学seo的培训学校
Python调用c++生成的dll
- 1.简单例子
- 1.1 vs2019 c++生成dll
- 1.2 Python端调用
- 2.调用c++类生成的dll
- 2.1 vs cpp端生成dll
- 2.2 Python端调用
- 参考文献
1.简单例子
1.1 vs2019 c++生成dll
- 项目中添加add.cpp文件
extern "C" int __declspec(dllexport) add(int x, int y)
{return x + y;
}
- 配置属性
- 生成dll
点击生成解决方案
,到输出目录文件夹查看add.dll
文件是否正常生成。
1.2 Python端调用
from ctypes import*import sys
try:mydll = cdll.LoadLibrary(r"add.dll")
except:sys.exit("No shared DLL/SO found")print(mydll.add(3,4))
#7
2.调用c++类生成的dll
2.1 vs cpp端生成dll
- 添加myDll.cpp文件
#include<iostream>
using namespace std;
class myDll
{
public:void helloDll() {cout << "hello dll" << endl;};
};extern "C"
{myDll obj;extern "C" _declspec(dllexport) void helloDll() {return obj.helloDll();}
}
- 配置属性
dll名设置为myDll
.
- 生成dll文件
参考第一个例子。
2.2 Python端调用
from ctypes import*import sys
try:mydll = cdll.LoadLibrary("myDll.dll")
except:sys.exit("No shared DLL/SO found")mydll.helloDll()#hello dll
测试通过!
参考文献
[1] python调用dll 结构体 python如何调用dll 转载