您的位置:首页 > 编程语言 > Python开发

在python 中使用 windows dll

2004-10-21 11:13 1291 查看
使用c/c++在windows上扩展python
如何在python 中使用 windows dll
首先下载 ctypes 模块 
说明在 http://starship.python.net/crew/theller/ctypes/reference.html
下面是我写的一个例子
首先写一个dll,源码如下

#include < stdio.h>
#include < windows.h>
//---------------------------------------
//演示使用DLL
//---------------------------------------
int APIENTRY __declspec(dllexport)  addnum1(int a){
return (a+1);

}
int APIENTRY __declspec(dllexport)  addnum10(int a,char *ca){
sprintf(ca,"%d",a+10);

return (a+10);

}

使用bcc 5.5 如下编译
bcc -tWD dll.c
在 python 中调用 如下:

from ctypes import *

fileName="dll.dll"
func=windll.LoadLibrary(fileName)
a =c_int(2)  #convert to c type
ret=c_int()
ret=func.addnum1(a)
print "value of a :",ret
b=c_int(20)
str=c_char_p("") #char point
ret=func.addnum10(b,str)
print "return value is :",ret
print "the string is  :",str.value
#windll.FreeLibrary(func)

输出结果为

value of a : 3
return value is : 30
the string is  : 30
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: