免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1979 | 回复: 0
打印 上一主题 下一主题

在python 中使用 windows dll [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-14 05:57 |只看该作者 |倒序浏览
如何在python 中使用 windows dll:
首先下载并安装
ctypes 模块
,说明参考
ctypes tutorial

使用示例:
test.c
#include stdio.h>
#include windows.h>
//---------------------------------------
//演示使用DLL
//---------------------------------------
__declspec(dllexport) int addnum1(int a)
{
    return (a+1);
}
__declspec(dllexport) int addnum10(int a,char *ca)
{
    sprintf(ca,"%d",a+10);
    return (a+10);
}
使用命令生成dll.(MinGW环境,详见
DLL Creation in MingW
;Visual C++6.0参见
Python调用c
)
gcc -c test.c
gcc -shared -o test.dll test.o -Wl,--out-implib,libtest.a
test.py
from ctypes import *
fileName="test.dll"
func=cdll.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
注意:
1. 调用的方式:windll与cdll
2. 编译时函数名换名



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8780/showart_184392.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP