• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python引用DLL文件的方法

python引用DLL文件的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python27.dll,python27.dll下载,python24.dll,python调用dll,python34.dll等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python引用DLL文件的方法。分享给大家供大家参考。具体分析如下:

在python中调用dll文件中的接口比较简单,如我们有一个test.dll文件,内部定义如下:

extern "C" 
{ 
int __stdcall test( void* p, int len) 
{  
return len; 
} 
}
</div>

在python中我们可以用以下两种方式载入

1.

import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )
</div>

2.

import ctypes
dll = ctypes.WinDll( 'test.dll' )
</div>

其中ctypes.windll为ctypes.WinDll类的一个对象,已经在ctypes模块中定义好的。在test.dll中有test接口,可直接用dll调用即可

nRst = dll.test( )
print nRst
</div>

由于在test这个接口中需要传递两个参数,一个是void类型的指针,它指向一个缓冲区。一个是该缓冲区的长度。因此我们要获取到python中的字符串的指针和长度

#方法一:
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = ctypes.c_char_p( )
pStr.value = sBuf
pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
nRst = dll.test( pVoid, len( pStr.value) )
</div>
#方法二:
test = dll.test
test.argtypes = [ctypes.c_char_p, ctypes.c_int]
test.restypes = ctypes.c_int
nRst = test(sBuf, len(sBuf))
</div>

如果修改test.dll中接口的定义如下:

extern "C" 
{ 
  int __cdecl test( void* p, int len)
  { 
    return len; 
  } 
}
</div>

由于接口中定义的是cdecl格式的调用,所以在python中也需要用相应的类型

1.  

import ctypes
dll = ctypes.cdll.LoadLibrary( 'test.dll' )
##注:一般在linux下为test.o文件,同样可以使用如下的方法: 
##dll =ctypes.cdll.LoadLibrary('test.o')
</div>

2. 

import ctypes
dll = ctypes.CDll( 'test.dll' )
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python引用DLL文件的方法
  • Python获取DLL和EXE文件版本号的方法
  • Python 调用DLL操作抄表机

相关文章

  • Python环境变量设置方法
  • Python装饰器decorator用法实例
  • 跟老齐学Python之总结参数的传递
  • python数据结构树和二叉树简介
  • python解析xml文件实例分析
  • Python的Django中django-userena组件的简单使用教程
  • 最大K个数问题的Python版解法总结
  • python回调函数用法实例分析
  • Python GAE、Django导出Excel的方法
  • python通过pil为png图片填充上背景颜色的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 介绍Python中几个常用的类方法
    • python计数排序和基数排序算法实例
    • Python实现Linux命令xxd -i功能
    • centos6.7安装python2.7.11的具体方法
    • 使用Python装饰器在Django框架下去除冗余代码的教程
    • 简单使用Python自动生成文章
    • python抓取网页中的图片示例
    • 在Python3中使用asyncio库进行快速数据抓取的教程
    • Flask框架的学习指南之用户登录管理
    • Python脚本实现下载合并SAE日志

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有