本文针对Windows平台下,python调取C/C++的dll文件。

1.如果使用C语言,代码如下,文件名为test.c。

__declspec(dllexport) int sum(int a,int b)
{
return (a + b);
}

如果使用C++语言,代码如下,文件名为test_cpp.cpp。在Windows平台下,__declspec(dllexport)是必须要添加的。

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT int sum(int a,int b)
{
return a + b;
}

2.编译生成dll文件。

在Visual Studio中,生成的dll文件有32bit和64bit两种,需要和python的版本对应上,否则将会报出“WindowsError: [Error 193] %1 不是有效的 Win32”这个错误,如下图所示。

我的本机上python为python 2.7.9(64bit),因此需要在Visual Studio中将工程属性设置为64位的。设置步骤如下所示,相应的64位dll在x64目录下生成。

由于有的电脑上并没有安装C++ Run Time,如只安装32bit的python,依然加载不了32位的dll,会报出如下错误,“WindowsError: [Error 126] ”

参考http://stackoverflow.com/questions/10411709/windowserror-error-126-when-loading-a-dll-with-ctypes这个链接,需要将运行库改为“MT”模式。

3.python调用,代码如下:

from ctypes import cdll

dll = cdll.LoadLibrary('G:\keyphrase extraction\Test\TestForPython.dll')
print dll.sum(1,2)

输出结果如下:

ctypes变量类型、C语言变量类型和Python语言变量类型之间的关系如下所示:

参考:http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/#icomments

FAQ:

1、如果只有一个dll,如何判断它是32位的还是64位的dll?

可以使用dumpbin工具,

在Visual Studio自带的dumpbin工具,然后输入dumpbin /HEADERS TestForPython.dll

machine后为(x64),可以看到这个dll是64位的;如果machin后为(x86),则为32位的dll。

参考链接:

http://wolfprojects.altervista.org/dllforpyinc.php

http://stackoverflow.com/questions/10411709/windowserror-error-126-when-loading-a-dll-with-ctypes

http://stackoverflow.com/questions/15374710/windowserror-error-193-1-is-not-a-valid-win32-application-in-python#

http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/#icomments

http://blog.csdn.net/jamestaosh/article/details/4237756

http://gashero.iteye.com/blog/519837

(转)python调取C/C++的dll生成方法的更多相关文章

  1. python调取C/C++的dll生成方法

    本文针对Windows平台下,python调取C/C++的dll文件. 1.如果使用C语言,代码如下,文件名为test.c. __declspec(dllexport) int sum(int a,i ...

  2. python 调取 shell 命令的几种方法

    os.system()无法获得到输出和返回值 os.popen()output = os.popen('cat /proc/cpuinfo')print output.read()返回的是 file ...

  3. Visual Studio 创建和使用dll的方法

    DLL是一个包含可由多个程序同时使用的代码和数据的库. DLL文件就是把一些函数导出来,然后在新程序中进行复用的过程. 第一部分:使用Visual Studio 2010进行DLL的制作 生成方法一: ...

  4. C++ 生成 dll 和调用 dll 的方法实例(转)

    1)生成dll 建立两个文件 xxx.h , xxx.cpp xxx.h内容如下: #ifdef BUILD_XXX_DLL#define EXPORT __declspec(dllexport)#e ...

  5. C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)

    原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 一.下载BarcodeLib.dll 下载地址 :http://do ...

  6. C# 利用BarcodeLib.dll生成条形码

    首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...

  7. 由动态库文件dll生成lib库文件(手动生成.def文件,然后使用lib命令编译,非常牛),同理可使用dll生成.a库文件

    本文基于OpenBlas的编译和安装,来说明如何从一个dll文件生成lib库文件. 参考OpenBlas的说明“Howto generate import library for MingW”,和Mi ...

  8. 将Qt 动态链接生成的exe及依赖dll打包方法

    源地址:http://blog.csdn.net/ztz0223/article/details/8939341 将Qt 动态链接生成的exe及依赖dll打包方法 原文:http://www.qtcn ...

  9. python 将png图片格式转换生成gif动画

    先看知乎上面的一个连接 用Python写过哪些[脑洞大开]的小工具? https://www.zhihu.com/question/33646570/answer/157806339 这个哥们通过爬气 ...

随机推荐

  1. web 开发之js---HTML5之广播聊天室

    那个头标题很有意思js做的 http://www.cnblogs.com/xgao/p/4200985.html

  2. LINQ to Tree - A Generic Technique for Querying Tree-like Structures,包含遍历WPF VisualTree

    https://www.codeproject.com/Articles/62397/LINQ-to-Tree-A-Generic-Technique-for-Querying-Tree#generi ...

  3. Unity 游戏对象消失 enable,destroy与active的区别

    gameObject.SetActive(false):是否在场景中停用该物体,停用后Hierarchy窗口呈灰色,用Find函数也找不到.如果该物体有子物体,要用SetActive Recursir ...

  4. 爬虫入门【3】BeautifulSoup4用法简介

    快速开始使用BeautifulSoup 首先创建一个我们需要解析的html文档,这里采用官方文档里面的内容: html_doc = """ <html>< ...

  5. bilingual evaluation understudy

    BLEU is designed to approximate human judgement at a corpus level, and performs badly if used to eva ...

  6. js网页视频播放: vcastr22 、 flowplayer 、 jwplayer

    实例结构: 实例1: demo.html <embed src="vcastr22.swf?vcastr_file=../wujiandao.flv" allowFullSc ...

  7. Activiti使用过程_1

    1 微信公众号:

  8. Computer Vision: Algorithms and ApplicationsのImage processing

    实在是太喜欢Richard Szeliski的这本书了.每一章节(after chapter3)都详述了该研究方向比較新的成果.还有很多很多的reference,假设你感兴趣.全然能够看那些參考论文 ...

  9. activiti基础--1------------------------生成.bpmn和.png以及部署流程定义

    helloworld.dbmn <?xml version="1.0" encoding="UTF-8"?> <definitions xml ...

  10. ionic新项目启动步骤

    1.sudo npm install -g gulp 2.SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install ...