我無法理解如何從Python中的現有DLL調用dll函數。在Python中調用DLL函數
OTAClient = cdll.LoadLibrary("C:\PATH\OTAClient.dll")
connect = OTAClientDLL.TDConnection()
exceptions.AttributeError: function 'TDConnection' not found
我讀的東西的名稱由編譯器被截斷。 有什麼建議嗎?
我無法理解如何從Python中的現有DLL調用dll函數。在Python中調用DLL函數
OTAClient = cdll.LoadLibrary("C:\PATH\OTAClient.dll")
connect = OTAClientDLL.TDConnection()
exceptions.AttributeError: function 'TDConnection' not found
我讀的東西的名稱由編譯器被截斷。 有什麼建議嗎?
該DLL實際上是一個COM DLL。 comtypes是需要使用這種類型的DLL而不是ctypes。 請確保download comtypes
from comtypes.client import CreateObject
OTAClientDLL = comtypes.client.GetModule("C:\PATH\OTAClient.dll")
這可能是因爲編譯器弄亂了函數名稱。有兩種方法可解決此問題:
閱讀以下(從http://docs.python.org/2/library/ctypes.html)
有時候,dll導出函數,其AREN名沒有有效的Python標識符,如「?? 2 @ YAPAXI @ Z」。在這種情況下,你必須使用GETATTR()來檢索功能:某些DLL不是導出函數名,而是以順序
>>>
>>> getattr(cdll.msvcrt, "[email protected]@Z")
<_FuncPtr object at 0x...>
>>>
在Windows上。這些功能可以通過與序號索引的dll對象來訪問:
>>>
>>> cdll.kernel32[1]
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "ctypes.py", line 310, in __getitem__
func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
如果你不知道什麼是功能名稱可能是,再看看LINK.EXE DUMPBIN.EXE。這些可以在Visual Studio安裝程序中找到,它們會轉儲dll中可用的所有功能。你可以在結果上運行一個grep。
我看過這個文檔之前,這沒有幫助。 – Kuews