python引用DLL文件的方法


Posted in Python onMay 11, 2015

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

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

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

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

1.

import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )

2.

import ctypes
dll = ctypes.WinDll( 'test.dll' )

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

nRst = dll.test( )
print nRst

由于在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) )
#方法二:
test = dll.test
test.argtypes = [ctypes.c_char_p, ctypes.c_int]
test.restypes = ctypes.c_int
nRst = test(sBuf, len(sBuf))

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

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

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

1.  

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

2. 

import ctypes
dll = ctypes.CDll( 'test.dll' )

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

Python 相关文章推荐
Python获取远程文件大小的函数代码分享
May 13 Python
从零开始学Python第八周:详解网络编程基础(socket)
Dec 14 Python
Python中函数及默认参数的定义与调用操作实例分析
Jul 25 Python
Python操作MySQL数据库的三种方法总结
Jan 30 Python
Python Selenium Cookie 绕过验证码实现登录示例代码
Apr 10 Python
TensorFlow损失函数专题详解
Apr 26 Python
python解决字符串倒序输出的问题
Jun 25 Python
python实现反转部分单向链表
Sep 27 Python
python接口自动化(十六)--参数关联接口后传(详解)
Apr 16 Python
django 2.2和mysql使用的常见问题
Jul 18 Python
Python数据正态性检验实现过程
Apr 18 Python
pytorch读取图像数据转成opencv格式实例
Jun 02 Python
深入解析Python中的WSGI接口
May 11 #Python
详细解析Python中__init__()方法的高级应用
May 11 #Python
从Python的源码来解析Python下的freeblock
May 11 #Python
详解Python的Django框架中的templates设置
May 11 #Python
Python素数检测的方法
May 11 #Python
Python中IPYTHON入门实例
May 11 #Python
Python使用MONGODB入门实例
May 11 #Python
You might like
php 根据url自动生成缩略图并处理高并发问题
2014/01/23 PHP
php计算给定时间之前的函数用法实例
2015/04/03 PHP
PHP中你应该知道的require()文件包含的正确用法
2015/06/12 PHP
如何使用php脚本给html中引用的js和css路径打上版本号
2015/11/18 PHP
PHP基于单例模式编写PDO类的方法
2016/09/13 PHP
php更新cookie内容的详细方法
2019/09/30 PHP
JQuery textlimit 显示用户输入的字符数 限制用户输入的字符数
2009/05/14 Javascript
增强用户体验友好性之jquery easyui window 窗口关闭时的提示
2012/06/22 Javascript
JavaScript 对任意元素,自定义右键菜单的实现方法
2013/05/08 Javascript
Javascript 按位与赋值运算符 (&=)使用介绍
2014/02/04 Javascript
jquery实现简单的遮罩层
2016/01/08 Javascript
微信小程序 小程序制作及动画(animation样式)详解
2017/01/06 Javascript
Axios学习笔记之使用方法教程
2017/07/21 Javascript
nodejs acl的用户权限管理详解
2018/03/14 NodeJs
浅谈ElementUI中switch回调函数change的参数问题
2018/08/24 Javascript
前后端如何实现登录token拦截校验详解
2018/09/03 Javascript
基于vue中keep-alive缓存问题的解决方法
2018/09/21 Javascript
javascript实现的字符串转换成数组操作示例
2019/06/13 Javascript
JS前端模块化原理与实现方法详解
2020/03/17 Javascript
简单介绍Python中的几种数据类型
2016/01/02 Python
对python中Matplotlib的坐标轴的坐标区间的设定实例讲解
2018/05/25 Python
Django contenttypes 框架详解(小结)
2018/08/13 Python
python 公共方法汇总解析
2019/09/16 Python
keras实现调用自己训练的模型,并去掉全连接层
2020/06/09 Python
Python pysnmp使用方法及代码实例
2020/08/24 Python
python 制作网站小说下载器
2021/02/20 Python
HTML5开发动态音频图的实现
2020/07/02 HTML / CSS
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
美国最大的在线生存商店:Survival Frog
2020/12/13 全球购物
捐款倡议书格式范文
2014/05/14 职场文书
学校火灾防控方案
2014/06/09 职场文书
雷人标语集锦
2014/06/19 职场文书
最美孝心少年事迹材料
2014/08/15 职场文书
活动总结模板大全
2015/05/11 职场文书
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL
Spring boot实现上传文件到本地服务器
2022/08/14 Java/Android