ctypes加载一个没有错误消息的DLL,但没有发生任何事情 [英] ctypes load a dll without error message, but nothing happened

查看:213
本文介绍了ctypes加载一个没有错误消息的DLL,但没有发生任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ctypes中使用windll.LoadLibrary将一个dll文件导入到python中。虽然没有任何错误消息,头文件中列出的任何函数似乎都没有被成功加载。我想知道dll文件是否有问题,或者我使用windll.LoadLibrary方法不正确。



可以从以下链接下载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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆