ctypes加载一个没有错误消息的DLL,但没有发生任何事情 [英] ctypes load a dll without error message, but nothing happened
问题描述
可以从以下链接下载dll和头文件:
http://www.cc.ncu.edu.tw/~auda/ATC3DG.rar
我使用的python命令是:
ctypes import *
libc = windll.LoadLibrary('ATC3DG .DLL')
可以从以下链接查看结果,其中显示dir(libc)不给我ATC3DG.h中列出的任何功能或变量:
http://www.cc.ncu.edu.tw/~auda/ATC3DG.jpg
我在Windows 7(64位)平台上使用python 2.7.3(32位)和ipython 0.13.1。
谢谢,
Erik Chang
除非您已经访问过该功能,否则使用 dir
时不会显示。例如:
在[98]中:从ctypes导入cdll
在[99]中:libc = cdll.LoadLibrary('libc.so.6')
在[100]中:dir(libc)
输出[100]:
['_FuncPtr',
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__ ',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__'
'__weakref__',
'_func_flags_',
'_func_restype_',
'_handle',
'_name']
在[ 101]:libc.printf
Out [101]:< _FuncPtr对象在0x65a12c0>
在[102]中:dir(libc)
输出[102]:
['_FuncPtr',
'__class__',
'__delattr__' ,
'__dict__',
'__doc__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
' _func_flags_',
'_func_restype_',
'_handle',
'_name',
'printf']
您可以通过查看 CDLL .__ getitem __
和 CDLL .__ getattr__
方法:
class CDLL(object):
#...
def __getattr __(self,name):
如果name.startswith('__')和name.endswith('__'):
提高AttributeError(name)
func = self .__ getitem __(name)
setattr(self,name,func)
return func
def __getitem __(self,name_or_ordinal) :
func = self._FuncPtr((name_or_ordinal,self))
如果不是isinstance(name_or_ordinal,(int,long)):
func .__ name__ = name_or_ordinal
return func
I tried to use windll.LoadLibrary in ctypes to import a dll file into python. Though there wasn't any error message, none of the functions listed in the header file seemed to be successfully loaded. I wonder if there is anything wrong with the dll file, or I have used the windll.LoadLibrary method incorrectly.
The dll and header files can be downloaded from the following link: http://www.cc.ncu.edu.tw/~auda/ATC3DG.rar
The python commands I used was:
from ctypes import *
libc=windll.LoadLibrary('ATC3DG.DLL')
The results can be viewed from the following link, which shows dir(libc) does not give me any functions or variables listed in ATC3DG.h:
http://www.cc.ncu.edu.tw/~auda/ATC3DG.jpg
I am using python 2.7.3 (32-bit), and ipython 0.13.1 on a windows 7 (64-bit) platform.
thanks,
Erik Chang
They don't show up when you use dir
unless you've already accessed the function. For example:
In [98]: from ctypes import cdll
In [99]: libc = cdll.LoadLibrary('libc.so.6')
In [100]: dir(libc)
Out[100]:
['_FuncPtr',
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_func_flags_',
'_func_restype_',
'_handle',
'_name']
In [101]: libc.printf
Out[101]: <_FuncPtr object at 0x65a12c0>
In [102]: dir(libc)
Out[102]:
['_FuncPtr',
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_func_flags_',
'_func_restype_',
'_handle',
'_name',
'printf']
You can see why this happens by looking at the CDLL.__getitem__
and CDLL.__getattr__
methods:
class CDLL(object):
# ...
def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
raise AttributeError(name)
func = self.__getitem__(name)
setattr(self, name, func)
return func
def __getitem__(self, name_or_ordinal):
func = self._FuncPtr((name_or_ordinal, self))
if not isinstance(name_or_ordinal, (int, long)):
func.__name__ = name_or_ordinal
return func
这篇关于ctypes加载一个没有错误消息的DLL,但没有发生任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!