ctypes load a dll without error message, but nothing happened

前端 未结 1 2007
遇见更好的自我
遇见更好的自我 2021-01-28 22:39

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 b

相关标签:
1条回答
  • 2021-01-28 23:10

    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
    
    0 讨论(0)
提交回复
热议问题