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

yipeiwu_com6年前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类继承用法实例分析

本文实例讲述了python类继承用法。分享给大家供大家参考。具体方法如下: #!/usr/bin/python # Filename: inherit.py class Schoo...

numpy.where() 用法详解

numpy.where() 用法详解

numpy.where (condition[, x, y]) numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condit...

Python之py2exe打包工具详解

下载Python对应版本的py2exe,使用这个工具可以将自己的程序打包成exe文件。 使用这个工具需要写一个用于打包的setup.py文件(名称可以自己定,不一定是setup.py),...

Python中为什么要用self探讨

接触Python以来,看到类里的函数要带个self参数,一直搞不懂啥麻子原因。晚上特别针对Python的self查了一下,理理。 Python要self的理由 Python的类的方法和普...

用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化

用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化

实例的背景说明 假定一个个人信息系统,需要记录系统中各个人的故乡、居住地、以及到过的城市。数据库设计如下: Models.py 内容如下:   from django...