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程序的执行原理
Apr 11 Python
详解python实现读取邮件数据并下载附件的实例
Aug 03 Python
python自动化脚本安装指定版本python环境详解
Sep 14 Python
Python3调用微信企业号API发送文本消息代码示例
Nov 10 Python
python中的二维列表实例详解
Jun 19 Python
Python爬虫框架Scrapy常用命令总结
Jul 26 Python
python 实现得到当前时间偏移day天后的日期方法
Dec 31 Python
在python中利用opencv简单做图片比对的方法
Jan 24 Python
python中round函数如何使用
Jun 19 Python
python 如何实现遗传算法
Sep 22 Python
Pyecharts 中Geo函数常用参数的用法说明
Feb 01 Python
python基于turtle绘制几何图形
Jun 15 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
用PHP生成自己的LOG文件
2006/10/09 PHP
php 获取远程网页内容的函数
2009/09/08 PHP
关于php内存不够用的快速解决方法
2013/10/26 PHP
PHP关联数组实现根据元素值删除元素的方法
2015/06/26 PHP
php使用preg_match()函数验证ip地址的方法
2017/01/07 PHP
jquery图片轮播插件仿支付宝2013版全屏图片幻灯片
2014/04/03 Javascript
js网页滚动条滚动事件实例分析
2015/05/05 Javascript
jQuery实现类似标签风格的导航菜单效果代码
2015/08/25 Javascript
js实现向右横向滑出的二级菜单效果
2015/08/27 Javascript
用JS实现图片轮播效果代码(一)
2016/06/26 Javascript
BootStrap表单验证实例代码
2017/01/13 Javascript
jQuery实现的简单在线计算器功能
2017/05/11 jQuery
深入理解AngularJs-scope的脏检查(一)
2017/06/19 Javascript
Element UI 自定义正则表达式验证方法
2018/09/04 Javascript
JS实现的贪吃蛇游戏完整实例
2019/01/18 Javascript
浅谈发布订阅模式与观察者模式
2019/04/09 Javascript
js的继承方法小结(prototype、call、apply)(推荐)
2019/04/17 Javascript
Vue项目从webpack3.x升级webpack4不完全指南
2019/04/28 Javascript
如何在Vue中抽离接口配置文件
2019/10/31 Javascript
js实现鼠标拖曳效果
2020/12/30 Javascript
[01:14:41]DOTA2-DPC中国联赛定级赛 iG vs Magma BO3第一场 1月8日
2021/03/11 DOTA
Python collections模块实例讲解
2014/04/07 Python
利用Python的Django框架中的ORM建立查询API
2015/04/20 Python
Python利用正则表达式实现计算器算法思路解析
2018/04/25 Python
浅谈移动端网页图片预加载方案
2018/11/05 HTML / CSS
wedgwood加拿大官网:1759年成立的英国国宝级陶瓷餐具品牌
2018/07/17 全球购物
Yahoo-PHP面试题2
2014/12/06 面试题
社团成立邀请函
2014/01/08 职场文书
保险专业求职信
2014/07/07 职场文书
收入及婚姻状况证明
2014/11/20 职场文书
你会写请假条吗?
2019/06/26 职场文书
人为什么会“幸灾乐祸”?
2019/08/06 职场文书
Python 如何安装Selenium
2021/05/06 Python
python实现A*寻路算法
2021/06/13 Python
纯html+css实现奥运五环的示例代码
2021/08/02 HTML / CSS
浅谈克隆 JavaScript
2021/11/02 Javascript