Windows系统Python直接调用C++ DLL的方法


Posted in Python onAugust 01, 2019

环境:Window 10,VS 2019, Python 2.7.12, 64bit

1,打开 VS 2019,新建C++ Windows 动态链接库工程 Example,加入下列文件,如果Python是64位的则在VS中 Solution platforms 选择 x64 编译成64位的 DLL;

Example.h

#pragma once
#ifndef CPP_EXPORTS
#define CPP_EXPORTS
#endif
#ifdef CPP_EXPORTS
#define CPP_API _declspec(dllexport)
#else 
#define CPP_API _declspec(dllimport)
#endif
#include <iostream>
using namespace std;
#ifdef __cplusplus
extern "C"
{
#endif
  CPP_API int __cdecl getInt();
  CPP_API const char* __cdecl getString();
  CPP_API void __cdecl setString(const char* str);
#ifdef __cplusplus
}
#endif

Example.cpp

#include "pch.h"
#include "Example.h"
CPP_API int __cdecl getInt()
{
  return 5;
}
CPP_API const char* __cdecl getString()
{
  return "hello";
}
CPP_API void __cdecl setString(const char* str)
{
  cout << str << endl;
}

编译,得到 Example.dll

2, 打开 Command,cd 到 Example.dll 所在目录,输入 Python2,进入python环境

>>> from ctypes import *
>>> dll = CDLL("Example.dll")
>>> print dll.getInt()
5
>>> getStr = dll.getString
>>> getStr.restype = c_char_p
>>> pChar = getStr()
>>> print c_char_p(pChar).value
hello
>>> setStr = dll.setString
>>> setStr.argtypes = [c_char_p]
>>> pStr = create_string_buffer("hello")
>>> setStr(pStr)
hello
-1043503984

总结

以上所述是小编给大家介绍的Windows系统Python直接调用C++ DLL的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
python实现数组插入新元素的方法
May 22 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
Jun 13 Python
从局部变量和全局变量开始全面解析Python中变量的作用域
Jun 16 Python
python简单读取大文件的方法
Jul 01 Python
Python爬取qq music中的音乐url及批量下载
Mar 23 Python
详解Python利用random生成一个列表内的随机数
Aug 21 Python
python__name__原理及用法详解
Nov 02 Python
python基于opencv检测程序运行效率
Dec 28 Python
通过Turtle库在Python中绘制一个鼠年福鼠
Feb 03 Python
Python 串口通信的实现
Sep 29 Python
如何在windows下安装配置python工具Ulipad
Oct 27 Python
Python使用pandas导入csv文件内容的示例代码
Dec 24 Python
Python CVXOPT模块安装及使用解析
Aug 01 #Python
Python Selenium 之数据驱动测试的实现
Aug 01 #Python
Python 一键获取百度网盘提取码的方法
Aug 01 #Python
Django中的静态文件管理过程解析
Aug 01 #Python
pycharm 批量修改变量名称的方法
Aug 01 #Python
python腾讯语音合成实现过程解析
Aug 01 #Python
Python空间数据处理之GDAL读写遥感图像
Aug 01 #Python
You might like
一个用于网络的工具函数库
2006/10/09 PHP
php中检查文件或目录是否存在的代码小结
2012/10/22 PHP
php简单实现短网址(短链)还原的方法(测试可用)
2016/05/09 PHP
Yii2中hasOne、hasMany及多对多关联查询的用法详解
2017/02/15 PHP
php识别翻转iphone拍摄的颠倒图片
2018/05/17 PHP
js 自定义的联动下拉框
2010/02/07 Javascript
在JavaScript中typeof的用途介绍
2013/04/11 Javascript
xmlhttp缓存清除的2种解决方法
2013/12/13 Javascript
JavaScript中的关联数组问题
2015/03/04 Javascript
基于JavaScript实现动态创建表格和增加表格行数
2015/12/20 Javascript
window.onload绑定多个事件的两种解决方案
2016/05/15 Javascript
js 获取元素所有兄弟节点的实现方法
2016/09/06 Javascript
layer子层给父层页面元素赋值,以达到向父层页面传值的效果实例
2017/09/22 Javascript
JS中双击和单击事件冲突的解决方法
2018/04/09 Javascript
Vue + Scss 动态切换主题颜色实现换肤的示例代码
2020/04/27 Javascript
如何在VUE中使用vue-awesome-swiper
2021/01/04 Vue.js
[01:03:22]LGD vs OG 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
使用Python判断IP地址合法性的方法实例
2014/03/13 Python
Python常用模块用法分析
2014/09/08 Python
用Python的Tornado框架结合memcached页面改善博客性能
2015/04/24 Python
Python计算三维矢量幅度的方法
2015/06/15 Python
Python使用当前时间、随机数产生一个唯一数字的方法
2017/09/18 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
2018/07/11 Python
Python实现正整数分解质因数操作示例
2018/08/01 Python
对pycharm 修改程序运行所需内存详解
2018/12/03 Python
Python进程间通信Queue消息队列用法分析
2019/05/22 Python
PyCharm 光标变成黑块的解决方式
2021/02/06 Python
微软瑞士官方网站:Microsoft瑞士
2018/04/20 全球购物
Manduka官网:瑜伽垫、瑜伽毛巾和服装
2018/07/02 全球购物
一套Java笔试题
2016/08/20 面试题
什么是GWT的Entry Point
2013/08/16 面试题
销售助理岗位职责
2014/02/21 职场文书
工程承诺书怎么写
2014/05/24 职场文书
领导班子在批评与自我批评座谈会上的发言
2014/09/28 职场文书
健康证明
2015/06/19 职场文书
游戏《铁拳》动画化!2022年年内播出
2022/03/21 日漫