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 14 Python
Python中的一些陷阱与技巧小结
Jul 10 Python
在Django的URLconf中使用命名组的方法
Jul 18 Python
python使用 HTMLTestRunner.py生成测试报告
Oct 20 Python
将TensorFlow的模型网络导出为单个文件的方法
Apr 23 Python
Tensorflow中使用tfrecord方式读取数据的方法
Jun 19 Python
Python定义二叉树及4种遍历方法实例详解
Jul 05 Python
python脚本调用iftop 统计业务应用流量的思路详解
Oct 11 Python
django 解决扩展自带User表遇到的问题
May 14 Python
pytorch 移动端部署之helloworld的使用
Oct 30 Python
详解pycharm自动import所需的库的操作方法
Nov 30 Python
解决pytorch 模型复制的一些问题
Mar 03 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
简单介绍下 PHP5 中引入的 MYSQLI的用途
2007/03/19 PHP
php 定界符格式引起的错误
2011/05/24 PHP
PHP队列用法实例
2014/11/05 PHP
php的socket编程详解
2016/11/20 PHP
js的隐含参数(arguments,callee,caller)使用方法
2014/01/28 Javascript
js实现上传图片预览的方法
2015/02/09 Javascript
详细解读Jquery各Ajax函数($.get(),$.post(),$.ajax(),$.getJSON())
2016/08/15 Javascript
微信小程序 视图层(xx.xml)和逻辑层(xx.js)详细介绍
2016/10/13 Javascript
javascript 定时器工作原理分析
2016/12/03 Javascript
JS中Array数组学习总结
2017/01/18 Javascript
微信小程序 this和that详解及简单实例
2017/02/13 Javascript
Babel 入门教程学习笔记
2018/06/13 Javascript
详解如何在Vue项目中导出Excel
2019/04/19 Javascript
开源一个微信小程序仪表盘组件过程解析
2019/07/30 Javascript
微信小程序进入广告实现代码实例
2019/09/19 Javascript
微信小程序scroll-view锚点链接滚动跳转功能
2019/12/12 Javascript
深入浅析vue全局环境变量和模式
2020/04/28 Javascript
js实现跳一跳小游戏
2020/07/31 Javascript
vue实现简易的双向数据绑定
2020/12/29 Vue.js
[52:36]VGJ.S vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
Python的一些用法分享
2012/10/07 Python
使用Python的Django框架实现事务交易管理的教程
2015/04/20 Python
Python爬虫通过替换http request header来欺骗浏览器实现登录功能
2018/01/07 Python
python实现顺序表的简单代码
2018/09/28 Python
Windows 安装 Anaconda3+PyCharm的方法步骤
2019/06/13 Python
Python Django的安装配置教程图文详解
2019/07/17 Python
Python利用全连接神经网络求解MNIST问题详解
2020/01/14 Python
物流毕业生个人的自我评价
2014/02/13 职场文书
报关报检委托书
2014/04/08 职场文书
药剂专业个人求职信范文
2014/04/29 职场文书
法人代表身份证明书及授权委托书
2014/09/16 职场文书
医院合作意向书范本
2015/05/08 职场文书
小学生暑假安全公约
2015/07/14 职场文书
go语言基础 seek光标位置os包的使用
2021/05/09 Golang
浅谈Golang 切片(slice)扩容机制的原理
2021/06/09 Golang
Python机器学习应用之基于线性判别模型的分类篇详解
2022/01/18 Python