Python 调用VC++的动态链接库(DLL)

yipeiwu_com5年前Python基础
1. 首先VC++的DLL的导出函数定义成标准C的导出函数:
复制代码 代码如下:

#ifdef LRDLLTEST_EXPORTS
#define LRDLLTEST_API __declspec(dllexport)
#else
#define LRDLLTEST_API __declspec(dllimport)
#endif

extern "C" LRDLLTEST_API int Sum(int a , int b);
extern "C" LRDLLTEST_API void GetString(char* pChar);

//a + b
LRDLLTEST_API int Sum(int a , int b)
{
return a + b;
}

//Get a string
LRDLLTEST_API void GetString(char* pChar)
{
strcpy(pChar, "Hello DLL");
}


2. Python中调用如下:
复制代码 代码如下:

from ctypes import *

fileName="LRDllTest.dll"
func=cdll.LoadLibrary(fileName)
str = create_string_buffer(20)
n = func.Sum(2, 3)
func.GetString(str)

print n
print str.raw

关于C语言中的一些参数类型详见:http://www.python.org/doc/2.5/lib/node454.html

3. 输出结果:
5
Hello DLL

相关文章

python 图片验证码代码分享

复制代码 代码如下: #coding: utf-8 import Image,ImageDraw,ImageFont,os,string,random,ImageFilter def i...

使用python绘制人人网好友关系图示例

代码依赖:networkx matplotlib 复制代码 代码如下: #! /bin/env python# -*- coding: utf-8 -*-import urll...

Python中str.format()详解

1. str.format 的引入 在 Python 中,我们可以使用 + 来连接字符串,在简单情况下这种方式能够很好的工作。但是当我们需要进行复杂的字符串连接时,如果依然使用 + 来...

利用Anaconda完美解决Python 2与python 3的共存问题

前言 现在Python3 被越来越多的开发者所接受,同时让人尴尬的是很多遗留的老系统依旧运行在 Python2 的环境中,因此有时你不得不同时在两个版本中进行开发,调试。 如何在系统中同...

在Python中使用Neo4j数据库的教程

在Python中使用Neo4j数据库的教程

 一个快速的REST例子 首先来看些基本知识。如果没有服务API,Neo4j就不能支持其他语言。该接口提供一组基于JSON消息格式的RESTful Web服务和一个全面的发现机...