使用Ctypes将CPP函数加载到Python中

时间:2013-03-27 16:49:24

标签: ctypes

我对Ctypes有疑问,不知道我做错了什么。是的,我是Python的新手,并在这里搜索其他帖子。所以任何建议都表示赞赏。

我想做什么:

我只是想将FXCM C ++ APP基金加载到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存在,我可能会做一些非常简单的错误,但不知道是什么。

1 个答案:

答案 0 :(得分:0)

您可以使用Dependency Walker找出正确的序列来手动加载DLL,或者只是将目录添加到系统搜索路径中:

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