请教Ctypesjava 调用c dlldll问题

后使用快捷导航没有帐号?
不通过xpcom技术,使用js-ctypes在addon中直接调用dll中的函数
js-ctypes 非常强大,是我们可以摆脱xpcom技术带来的版本兼容性束缚!这里我们先提供一个最简单的例子来说明如何使用ctypes:
更多信息请参考MDN相关文献
欢迎有兴趣和疑问的同学请回帖。Components.utils.import(&resource://gre/modules/ctypes.jsm&);&&var lib = ctypes.open(&C:\\WINDOWS\\system32\\user32.dll&);&&/* Declare the signature of the function we are going to call */&&var msgBox = lib.declare(&MessageBoxW&,& && && && && && && && && &ctypes.winapi_abi,& && && && && && && && && &ctypes.int32_t,& && && && && && && && && &ctypes.int32_t,& && && && && && && && && &ctypes.jschar.ptr,& && && && && && && && && &ctypes.jschar.ptr,& && && && && && && && && &ctypes.int32_t);&&var MB_OK = 0;&&var ret = msgBox(0, &Hello world&, &title&, MB_OK);&&lib.close();复制代码
另外,也可以用javascript直接编写XPCOM同样可以避免火狐版本不兼容的问题。
回复 xulboy: 介绍js-ctypes技术,官方的demo足够了&
回复 鸡爪子: 从目前来看,xpcom技术还是火狐的核心技术之一,是不会在短时间内抛弃的。只是这个技术和火狐的版本依赖很严重,适合火狐内部的组件使用,不适合火狐扩展开发,所以不建议扩展使用这个技术。&
这个就是官网的例子,发个自己写的吧...比如如何获取鼠标现在的位置,谢谢&
xpcom是不是火狐不支持了。是不是现在都用add-on来做扩展了
Firefox 插件与扩展开发,线上交流群。分享我们的经验。 QQ群号: 让我们的问题能得到及时的解决。 诚邀高手。 欢迎新手。
为什么我运行官网的这个例子,会出现Components.utils.import(&resource://gre/modules/ctypes.jsm&);ctypes.jsm没有定义呢?
还有你用的是哪个版本的火狐?你有没有完整的代码示例?&
你把浏览器原始的错误信息贴上来看看&Python调用C/C++动态链接库的方法详解
转载 & & 投稿:shichen2014
这篇文章主要介绍了Python调用C/C++动态链接库的方法,需要的朋友可以参考下
本文以实例讲解了Python调用C/C++ DLL动态链接库的方法,具体示例如下:
首先,在创建一个DLL工程(本例创建环境为VS 2005),头文件:
#ifdef EXPORT_HELLO_DLL
#define HELLO_API __declspec(dllexport)
#define HELLO_API __declspec(dllimport)
extern "C"
HELLO_API int IntAdd(int , int);
//hello.cpp
#define EXPORT_HELLO_DLL
#include "hello.h"
HELLO_API int IntAdd(int a, int b)
return a +
这里有两个注意点:
(1)弄清楚编译的时候函数的调用约定采用的__cdecl还是__stdcall,因为根据DLL中函数调用约定方式,Python将使用相应的函数加载DLL。
(2)如果采用C++的工程,那么导出的接口需要extern "C",这样python中才能识别导出的函数。
我的工程中采用__cdecl函数调用约定方式进行编译链接产生hello.dll,然后Python中采用ctypes库对hello.dll进行加载和函数调用:
from ctypes import *
dll = cdll.LoadLibrary('hello.dll');
ret = dll.IntAdd(2, 4);
至此,第一个小例子已经完成了,读者可以自己动手尝试一下运行效果。
示例一只是一个"hello world"级别的程序,实际运用中更多的需要传递数据结构、字符串等,才能满足我们的需求。那么本示例将展示,如何传递数据结构参数,以及如何通过数据结构获取返回值。
首先编写DLL工程中的头文件:
#ifdef EXPORT_HELLO_DLL
#define HELLO_API __declspec(dllexport)
#define HELLO_API __declspec(dllimport)
#define ARRAY_NUMBER 20
#define STR_LEN 20
struct StructTest
char str[STR_LEN];
int iArray[ARRAY_NUMBER];
extern "C"
//HELLO_API int IntAdd(int , int);
HELLO_API char* GetStructInfo(struct StructTest* pStruct);
CPP文件如下:
//hello.cpp
#include &string.h&
#define EXPORT_HELLO_DLL
#include "hello.h"
HELLO_API char* GetStructInfo(struct StructTest* pStruct)
for (int i = 0; i & ARRAY_NUMBER; i++)
pStruct-&iArray[i] =
pStruct-&pChar = "hello python!";
strcpy (pStruct-&str, "hello world!");
pStruct-&number = 100;
return "just OK";
GetStructInfo这个函数通过传递一个StructTest类型的指针,然后对对象中的属性进行赋值,最后返回"just OK".
编写Python调用代码如下,首先在Python中继承Structure构造一个和C DLL中一致的数据结构StructTest,然后设置函数GetStructInfo的参数类型和返回值类型,最后创建一个StructTest对象,并将其转化为指针作为参数,调用函数GetStrcutInfo,最后通过输出数据结构的值来检查是否调用成功:
from ctypes import *
ARRAY_NUMBER = 20;
STR_LEN = 20;
#define type
INTARRAY20 = c_int * ARRAY_NUMBER;
CHARARRAY20 = c_char * STR_LEN;
#define struct
class StructTest(Structure):
_fields_ = [
("number", c_int),
("pChar", c_char_p),
("str", CHARARRAY20),
("iArray", INTARRAY20)
#load dll and get the function object
dll = cdll.LoadLibrary('hello.dll');
GetStructInfo = dll.GetStructI
#set the return type
GetStructInfo.restype = c_char_p;
#set the argtypes
GetStructInfo.argtypes = [POINTER(StructTest)];
objectStruct = StructTest();
#invoke api GetStructInfo
retStr = GetStructInfo(byref(objectStruct));
#check result
print "number: ", objectStruct.
print "pChar: ", objectStruct.pC
print "str: ", objectStruct.
for i,val in enumerate(objectStruct.iArray):
print 'Array[i]: ',
print retS
1. 用64位的Python去加载32位的DLL会出错
2. 以上只是些测试程序,在编写Python过程中尽可能的使用"try Except"来处理异常
3. 注意在Python与C DLL交互的时候字节对齐问题
4. ctypes库的功能还有待继续探索
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Python x64下ctypes动态链接库出现access violation的原因分析
access violation error in Python x64 when using ctypes (caused by pointer)问题描述似乎这个问题仅存在于Windows下Python x64中,且Python 2和Python 3中的表现不一致;并且一般是由于调用的C-API包含有指针传递,出现类似如下错误OSError: exception: access violation reading 0xF0FA60示例C++代码:#include &string&
struct Foo
char* child = &a child&;
extern &C&
__declspec(dllexport) char* bar(char*, char*);
__declspec(dllexport) Foo* getFoo(void);
__declspec(dllexport) char* getChild(Foo*);
char* bar(char* a, char* b)
char* out = new char[strlen(a) + strlen(b) + 1];
strcpy(out, a);
strcat(out, b);
Foo* getFoo()
return new Foo();
char* getChild(Foo* foo)
return foo-&
}函数bar()期望完成的是两个字符串的连接。getFoo()返回一个类指针,而getChild()接受一个类指针,返回字符串。测试一正常的Python代码:import ctypes
DLL_PATH = 'C:/Test/x64/Release/Test.dll'
lib = ctypes.cdll.LoadLibrary(DLL_PATH)
a = b'wow'
b = b'python'
out = lib.bar(a, b)
text_out = ctypes.string_at(out)
print(text_out)在Python 2.7.12下执行正确,返回wowpython但在Python 3.5.2下执行会提示错误:Traceback (most recent call last):
File &C:\Desktop\test_x64.py&, line 9, in &module&
text_out = ctypes.string_at(out)
File &C:\Python35\lib\ctypes\__init__.py&, line 491, in string_at
return _string_at(ptr, size)
OSError: exception: access violation reading 0xAF60ctypes.string_at(out)出现了非法的内存地址访问。测试二正常的Python代码:import ctypes
DLL_PATH = 'C:/Test/x64/Release/Test.dll'
lib = ctypes.cdll.LoadLibrary(DLL_PATH)
foo = lib.getFoo()
out = lib.getChild(foo)
text_out = ctypes.string_at(out)
print(text_out)在Python 2和Python 3中都会出现非法的内存访问,但出错的语句不一样。在Python 2.7.12下执行提示错误:Traceback (most recent call last):
File &C:\Desktop\test_x64_2.py&, line 8, in &module&
text_out = ctypes.string_at(out)
File &C:\Python27\lib\ctypes\__init__.py&, line 506, in string_at
return _string_at(ptr, size)
WindowsError: exception: access violation reading 0xFFFFFFFFctypes.string_at(out)出现了非法的内存地址访问。在Python 3.5.2下执行提示错误:Traceback (most recent call last):
File &C:\Desktop\test_x64_2.py&, line 7, in &module&
out = lib.getChild(foo)
OSError: exception: access violation reading 0xFFFFFFFFDCCDD220lib.getChild(foo)出现了非法的内存地址访问。原因分析先来看看ctypes.string_at(),在官方文档里给出的描述是ctypes.string_at(address, size=-1)This function returns the C string starting at memory address address as a bytes object. If size is specified, it is used as size, otherwise the string is assumed to be zero-terminated.string_at()接受的参数是内存地址,似乎隐约知道出错的原因了:给string_at()传递了一个错误的内存地址,导致程序执行时试图访问非法的内存地址,提示错误。但传入string_at()的参数是由API直接返回的,怎么会出错呢?继续看官方文档,官方文档中提到在加载动态链接库时Functions in these libraries use the standard C calling convention, and are assumed to return int.意思就是C-API中任何类型的返回值,在Python中统一都是int,这样也容易理解:返回int类型就是int值,返回指针就是内存地址(毕竟指针实际就是记录内存地址的)。那是不是返回的指针地址就是错的?我们来试试看。现在打印出返回的内存地址import ctypes
DLL_PATH = 'C:/Test/x64/Release/Test.dll'
lib = ctypes.cdll.LoadLibrary(DLL_PATH)
a = b'wow'
b = b'python'
out = lib.bar(a, b)
print('address:', out)
text_out = ctypes.string_at(out)
print(text_out)在Python 3.5.2运行几次,看看返回的内存地址和错误提示的关系address: -
OSError: exception: access violation reading 0xFFFFFFFF9880EA20
address: -
OSError: exception: access violation reading 0xFFFFFFFFD07E1450
OSError: exception: access violation reading 0xBA2890返回的内存地址和非法内存地址访问是一致的!而且内存地址甚至出现了负数,怎么可能!问题就是出在返回指针(即返回内存地址)上面了,无论是char*还是Foo*都有这种问题!通过测试32位版本的动态链接库,Python 2和Python 3均未出现问题,可以确定是32位和64位兼容性问题,类似于著名的C语言x86和x64下int的长度问题。注意:限于自己目前的水平,更深层次的原因还不得而知,但需要注意Python2和Python3下出错的表现不一致,Python2下似乎可以克服一些指针传递问题。解决方法通过多次尝试,我发现可以通过设置API的restype来获得正确的内存地址。restype可以重载API的返回值类型(记得上面说的默认值为int吧),因为64位下内存地址为64位无符号型整数,因此设置为ctypes.c_uint64。由于修改了返回值类型,在指针作为函数调用的输入参数时,也可能要作出对应的类型转换。修正后的测试一import ctypes
DLL_PATH = 'C:/Test/x64/Release/Test.dll'
lib = ctypes.cdll.LoadLibrary(DLL_PATH)
a = b'wow'
b = b'python'
lib.bar.restype = ctypes.c_uint64
# 修改lib.bar返回类型
out = lib.bar(a, b)
text_out = ctypes.string_at(out)
print(text_out)Python 2.7.12和Python 3.5.2均能得到正确结果。修正后的测试一import ctypes
DLL_PATH = 'C:/Test/x64/Release/Test.dll'
lib = ctypes.cdll.LoadLibrary(DLL_PATH)
lib.getFoo.restype = ctypes.c_uint64
# 修改lib.getFoo返回类型
foo = lib.getFoo()
lib.getChild.restype = ctypes.c_uint64
# 修改lib.getChild返回类型
out = lib.getChild(ctypes.c_uint64(foo))
# 指针作为输入也要进行类型转换
text_out = ctypes.string_at(out)
print(text_out)Python 2.7.12和Python 3.5.2均能得到正确结果。小结如上提到的解决方法只能算作临时的修补,我更倾向于认为这是Python自身的一个bug:没有解决64位环境下的C指针类型问题。参考
添加新评论问一个关于python调用dll的问题_python吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:166,119贴子:
问一个关于python调用dll的问题收藏
在调用dll文件时,可以直接调用dll文件中在寄存器里存储的内容么?因为是新手,可能表述的不是很清楚。举个例子:from ctypes import *fileName="USB3xIII.dll"func=windll(fileName)func.getxtarget(Motor) 函数getxtarget返回的值为电机的当前状态,但是我想取得在函数中的电机的运行速度,比如叫x_target。请问可以直接用么?比如func.setvtarget(x_target)谢谢
阿里云云市场镜像可以一键部署云服务器所需要的运行环境和个性化的软件应用,满足建站,应用开发,可视化管理等个性化需求,python镜像让云服务器&即开即用&.
没人知道么?是不是我问的本身就不对呀?
登录百度帐号推荐应用参考资料:http://www.tech-q.cn/thread-.html
http://docs.python.org/library/ctypes.html#return-types
今天研究了下python调用C的动态库的方法,查阅了相关资料后,发现调用还是很方便的。
&--------------------------------------------------------------------------------
#!/usr/bin/python
from ctypes import *
def SupportSuff(value):
if value == 0:
raise Exception, 'Not support file type.'
return value
def main():
#libtest = cdll.LoadLibrary('./libsuffix.so')
libtest = CDLL('./libsuffix.so')
get_file_suffix = libtest.get_file_suffix
# 设定好调用函数的参数类型和返回值类型
get_file_suffix.argtypes = [POINTER(c_char)]
get_file_suffix.restype = c_char_p
name = 'test.ppt'
#直接用方法名进行调用
suff = get_file_suffix(name)
print suff
#如果函数的返回值是整数,可以设定其返回属性restype为一个可以调用的函数或类。这样该函数的返回值就是你所指定的函数的结果。
# 主要用来检查返回值,然后自动抛出异常(其实就是将手动检查返回值改为异常抛出的方式)
libtest.check_file_suffix.restype = SupportSuff
ret = libtest.check_file_suffix(suff)
& 具体调用代码如上,使用起来也还是比较简单的。还有个问题就是关于内存传递使用的问题。无论是在C里申请内存,在python用;还是python申请内存,在C里使用。
1、在C里申请内存,在py里调用后,需要释放的话 得在py里调用C的释放函数;(我没做过实验,只是在网上找资料的过程中看到有人这样说)
2、在py里申请内存,传递给C去操作,此时的内存释放是不需要去管的,py申请的内存自动由py释放了。
http://osdir.com/ml/python.ctypes/2005-07/msg00014.html
& Just a quick question I did not see answered in the ctypes tutorial:
& when I create a string buffer, can I assume that python garbage
& collects it, or do I need to call a special method to free it?
& p = create_string_buffer("", 254)
& #... processing code, finished using p
& # do I need to 'release' p?
You don't have to take a special action. When the last reference from
Python to the string buffer goes away the memory is freed. That's true
for all ctypes instances.
其实遇到的很多问题在py的官方文档里都有介绍了,这里有一篇中文翻译的:
关于ctypes在文档中也有一些比较特别有意思的地方,如:
注意 这ctypes不包含面向对象,它每次查看一个属性时就创建一个全新的等价的对象。&&& pi.contents is iFalse&&& pi.contents is pi.contentsFalse&&&
具体可在文档中去查询,py作为一个胶水语言,这种调用别的语言的活应该是经常碰到的,应该用熟。
在网上找py的资料的时候发现,质量比较高的提问和回答经常出自CPyUG的Google group邮件列表,虽然得FQ才能访问,不过内容都挺有价值的。
阅读(...) 评论()

我要回帖

更多关于 python 调用dll 的文章

 

随机推荐