您的位置:首页 > 编程语言 > C语言/C++

使用Visual Studio,几步实现Python C++扩展,以及DLL调用

2015-05-26 14:16 1201 查看
在网上搜了下Python扩展教程,很多提到第三方开源库,而官方推荐的是用setup.py。其实用Visual Studio很简单!来看一下如何一步步编写一个Python扩展,以及如何通过扩展来调用DLL。

参考原文:Wrapping C/C++ Methods of Dynamsoft Barcode SDK for Python


Visual Studio环境配置

在Visual Studio中创建一个Win32工程DynamsoftBarcodeReader。打开工程属性,添加头文件和库文件路径:









添加依赖库python27.lib:





把扩展改成.pyd:





这样就可以去build工程了。不过如果是使用Debug去build,会出现错误:





原因是因为官方发布的python安装包不包涵debug的库,如果需要可以自己下载源码编译。所以把配置切换到release,成功生成DynamsoftBarcodeReader.pyd:






DLL调用举例:封装Dynamsoft Barcode SDK C++接口<
4000
br style="padding:0px; margin:0px">

在Python脚本中导入DynamsoftBarcodeReader,Python会搜索DynamsoftBarcodeReader.pyd,并且调用initDynamsoftBarcodeReader()做初始化。





在工程配置中先加入Dynamsoft Barcode SDK的头文件和库路径,然后使用下面的代码调用Barcode解码,并把结果转换成Python数据:

在工程配置中加入DLL自动拷贝,在编译之后就可以把DLL拷贝到release目录中了:





编译工程生成DynamsoftBarcodeReader.pyd。

在release目录中新建一个Python脚本:





一个简单的Python Barcode Reader代码如下:

用Barcode图片测试一下:






源码

https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Python
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Barcode CC++ Python