<>我试图从已经编译的(不由我)C++的DLL文件加载和调用函数。在
我面临两个问题:使用CDLL()加载DLL:from ctypes import *
__path_aa_Fn_dll__ = r'C:\user\aacad_2017\aa_Functions.dll' # DLL file path
aa_Fn_dll = CDLL(__path_aa_Fn_dll__) # load DLL
这给了我模块找不到错误:Traceback (most recent call last):
File "C:/user/aacad_2017/cadClass.py", line 189, in
aa_Fn_dll = CDLL(__path_aa_Fn_dll__)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\ctypes\__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
使用win32加载同一DLLapi.LoadLibraryEx()然后是CDLL()aa_Fn_dll_handle = win32api.LoadLibraryEx(__path_aa_Fn_dll__, 0, win32con.LOAD_LIBRARY_AS_DATAFILE) # get DLL handle
aa_Fn_dll = CDLL(__path_aa_Fn_dll__, handle=aa_Fn_dll_handle) # load DLL
它能够加载DLL。在
但是,尝试在此DLL中调用函数时,会引发以下AttributeError:aa_Fn_dll.aaBIN2iv.restype = c_double # set function return type
aa_Fn_dll.aaBIN2iv.argtypes = [c_double, c_double, c_double, c_double, c_double, c_double, c_double, c_char_p] # set function argument types
aa_Fn_dll.aaBIN2iv(10, 10, 10, 10, 10, 10, 10, b"C") # call DLL function
Traceback (most recent call last):
File "C:/user/aacadMatlabWrap_2017_new_test/cadClass.py", line 196, in
aa_Fn_dll.aaBIN2iv.restype = c_double
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\ctypes\__init__.py", line 361, in __getattr__
func = self.__getitem__(name)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\ctypes\__init__.py", line 366, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'aaBIN2iv' not found
经过广泛的在线研究和查阅Ctypes文档,我尝试了以下历史解决方案:使用依赖关系查询器检查依赖关系
确保所有DLL依赖项都放在同一目录中
将DLL目录添加到系统路径确保将{{CD1}}添加到C++头文件中,防止名称“窜改”< /LI>在C++头文件中使用关键字^ {CD2}}
在加载DLL之前,将Python中的当前工作目录设置为DLL文件目录的当前工作目录
没有成功。。。在<>我还编译了自己的64位C++ DLL文件(完全独立于AAI)函数.dll在微软的visualstudio2010中,在调试和发布两种模式下,包含测试函数。在
此测试DLL能够加载,并成功调用其函数,如下所示:__path_test_dll__ = r'C:\user\dllTest\x64\Release\dllTest.dll' # test DLL file path
test_dll = CDLL(__path_test_dll__) # load test DLL
test_dll.fn4.restype = c_void_p # set test DLL function return type
test_dll.fn4.argtypes = [c_double, c_double, c_double, c_char_p, c_bool] # set test DLL function argument types
test_dll.fn4(10, 12, 1, b"Pay", True) # make test DLL function call
使用预期的控制台输出:Min_P * Rate == $10
Pay: 1 Min_P: 10 Max_P: 12 Iteration #: 1
Min_P * Rate == $11
Pay: 0 Min_P: 11 Max_P: 12 Iteration #: 2
Min_P * Rate == $12
Pay: 1 Min_P: 12 Max_P: 12 Iteration #: 3
Process finished with exit code 0
我已经花了几天时间在这个问题上,如果能在正确的方向上指出一点,我将不胜感激。在