2013-03-27 16 views
-1

我對Ctypes有個疑問,不知道我做錯了什麼。是的,我是Python的新手,並在這裏搜索其他帖子。所以,任何意見表示讚賞。使用Ctypes將CPP函數加載到Python中

什麼我想做的事:

我只是想給FXCM C++ APP fundtions加載到的Python 3.3,所以我可以叫他們連接到他們的服務器。 看起來Ctypes似乎是最好的工具。因此,一個簡單的代碼在Python:

import os 
dirlist = os.listdir('ForexConnectAPIx64/bin') 
from pprint import pprint 
pprint(dirlist) 


from ctypes import * 
myDll = cdll.LoadLibrary ("ForexConnectAPIx64/bin/ForexConnect.dll") 

給出了結果:

Traceback (most recent call File "C:\Users\scaberia3\Python_Projects  \FTC\ListDir_Test.py", line 20, in <module> 
myDll = cdll.LoadLibrary ("ForexConnectAPIx64/bin/ForexConnect.dll") 
File "C:\Python33\lib\ctypes\__init__.py", line 431, in LoadLibrary 
return self._dlltype(name) 
File "C:\Python33\lib\ctypes\__init__.py", line 353, in __init__ 
self._handle = _dlopen(self._name, mode) 
OSError: [WinError 126] Das angegebene Modul wurde nicht gefunden (Module not found) 

['ForexConnect.dll', 
'fxmsg.dll', 
'gsexpat.dll', 
'gslibeay32.dll', 
'gsssleay32.dll', 
'gstool2.dll', 
'gszlib.dll', 
'java', 
'log4cplus.dll', 
'msvcp80.dll', 
'msvcr80.dll', 
'net', 
'pdas.dll']     

指的路徑是正確的ForextConnect.dll存在,並且我可能會做一些很簡單的錯誤,但不知道是什麼。

回答

0

您可以使用Dependency Walker中找出正確的順序手動加載的DLL,或者乾脆目錄添加到系統的搜索路徑:

dllpath = os.path.abspath('ForexConnectAPIx64/bin') 
os.environ['PATH'] += os.pathsep + dllpath 
myDLL = CDLL('ForexConnect.dll') 
+0

謝謝,我在裝都嘗試,還是問題。該DLL是未知的。 – 2013-03-28 20:44:04

+0

如果您已經將包含所有DLL的目錄添加到系統'PATH'並且仍然出現加載錯誤,(1)確保您使用的是64位Python,如果是,(2)檢查使用x64 Dependency Walker的DLL查看無法找到哪些DLL依賴關係。 – eryksun 2013-03-28 21:59:40