用python反思DLL

时间:2009-10-29 11:00:42

标签: python ctypes

我目前正在尝试使用python对DLL进行一些内省。我想自动创建一个基于DLL的图形测试界面。

我可以很容易地在python中加载我的DLL,并调用一些函数。主要的问题是如果我在没有调用任何方法的情况下在对象上调用“dir”,我就得到了结果

>>> dir(myLib)
['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
'__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__i
nit__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__s
etattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_fl
ags_', '_func_restype_', '_handle', '_name']

当我手动调用一个函数(如“Read_Version”)时,我有dir函数的结果

>>> dir(myLib)
['Read_Version', '_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
'__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__i
nit__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__s
etattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_fl
ags_', '_func_restype_', '_handle', '_name']

似乎内省只对我已经调用过的函数起作用,这实际上不是“有用”;)。

您是否有其他想法来获取DLL中的函数? (当然是在python中)

我在Windows下使用python 2.6。

1 个答案:

答案 0 :(得分:2)

据我所知,没有简单的方法可以做到这一点。您必须使用一些外部工具(例如link /dump /exports)或使用PE / DLL解析器(例如pefile)。