在python 中使用 windows dll

 
 
  
  使用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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值