本文实例讲述了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调用即可
立即学习“Python免费学习笔记(深入)”;
nRst = dll.test( ) print nRst
由于在test这个接口中需要传递两个参数,一个是void类型的指针,它指向一个缓冲区。一个是该缓冲区的长度。因此我们要获取到python中的字符串的指针和长度
该软件是一个以asp+access进行开发的适用于传媒类公司的企业网站源码。安装方法:1、所有文件传至网站目录2、浏览器执行http://你的访问网址/install3、输入mysql帐号及密码信息,提交安装后台地址:http://你的访问网址/admin.php默认账号:admin默认密码:123123
0
#方法一: 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程序设计有所帮助。
.dll文件缺失怎么办?.dll文件在哪下载?不用担心,这里为大家提供了所有的.dll文件下载,无论用户丢失的是什么.dll文件,在这里都能找到。用户保存后,在网盘搜索dll文件全称即可查找下载!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号