Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ctypes-获取CFUNCTYPE的名称_Python_Dll_Ctypes - Fatal编程技术网

Python ctypes-获取CFUNCTYPE的名称

Python ctypes-获取CFUNCTYPE的名称,python,dll,ctypes,Python,Dll,Ctypes,我正在研究一些我没有用pdb编写的代码 (Pdb) self.function <CFunctionType object at 0x000000DC768E0E18> (Pdb) type(self.function) <class 'ctypes.CFUNCTYPE.<locals>.CFunctionType'> (Pdb) dir(self.function) ['__bool__', '__call__', '__class__', '__ctype

我正在研究一些我没有用pdb编写的代码

(Pdb) self.function
<CFunctionType object at 0x000000DC768E0E18>
(Pdb) type(self.function)
<class 'ctypes.CFUNCTYPE.<locals>.CFunctionType'>
(Pdb) dir(self.function)
['__bool__', '__call__', '__class__', '__ctypes_from_outparam__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_argtypes_', '_b_base_', '_b_needsfree_', '_flags_', '_objects', '_restype_', 'argtypes', 'errcheck', 'restype']
(Pdb) self.function._flags_
1
(Pdb) self.function._objects
{'0': <CDLL 'C:\path', handle 7ffe26465400 at 0xdc74ad2908>}
我能找出自我功能的定义吗

不,因为它是在一些用C(或其他语言)编写的源代码中定义的,而您甚至没有(或者您有,但Python不知道它在哪里);您所拥有的只是编译它所产生的DLL/so/dylib


…或者函数的名称是什么

对。通常,您可以通过与Python中的许多其他对象相同的方式获得ctypes C函数的名称:通过
\uuu name\uu
特殊属性:

>>> import ctypes
>>> libm = ctypes.CDLL('libm.dylib')
>>> fabs = libm.fabs
>>> fabs
<_FuncPtr object at 0x1067ac750>
>>> fabs.__name__
'fabs'
>>> fabs._objects['0']._name
'libm.dylib'
(尽管有下划线,但正如文档所解释的,这是一个公共属性。)

这通常是传递给
CDLL
构造函数或
CDLL.LoadLibrary
调用的名称。在Windows上,对于加载了
cdll.spam
magic的库,我认为您实际上得到了解析的路径名,比如
'D:\path\to\spam.dll'
,而不仅仅是
'spam'
'spam.dll'
,但我不是肯定的


此外,调用函数时是否可以单步执行该函数

否,因为该函数是编译的机器代码;它没有任何Python字节码供您使用


当然,您可以附加一个调试器,如Visual Studio、lldb或gdb,然后以这种方式进入机器代码。

您不能在Python调试器中进入C函数,因为它没有任何Python字节码可进入。(当然,您可以附加gdb、lldb或visualstudio之类的调试器并单步执行机器代码,如果您想这样做的话。)是否可以获取函数名?
>>> fabs._objects['0']._name
'libm.dylib'