Python Ctypes加载第三方C++ DLL遇加载失败及函数找不到问题
I've spent days stuck on two frustrating issues while trying to load and invoke functions from a third-party C++ DLL that I didn't compile myself. Let me break down exactly what's happening:
Scenario 1: OSError: [WinError 126] When Loading DLL with CDLL()
When I try to load the DLL directly using ctypes.CDLL(), I get a "specified module not found" error. Here's the code I'm using:
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
Full error traceback:
Traceback (most recent call last): File "C:/user/aacad_2017/cadClass.py", line 189, in <module> 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
Scenario 2: AttributeError: Function 'aaBIN2iv' Not Found After Successful Loading
I tried a workaround using win32api.LoadLibraryEx() to get a handle first, then loading the DLL with CDLL() using that handle. The DLL loads successfully this time, but when I try to call the aaBIN2iv function, I hit an attribute error.
Code to load the DLL:
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
Code to call the function + resulting error:
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 <module> 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
Attempted Solutions (All Unsuccessful)
I've tried every fix I can think of, but none have resolved the issues:
- Used Dependency Walker to check for missing dependencies
- Placed all dependent DLLs in the same directory as the target DLL
- Added the DLL's directory to the system
PATHenvironment variable - Verified the C++ header files use
extern "C"to prevent name mangling - Confirmed the header files use an
EXPORTED_FUNCTIONkeyword for function visibility - Set Python's working directory to the DLL's location before loading
Working Test Case (Self-Compiled DLL)
As a sanity check, I compiled my own 64-bit C++ test DLL using Microsoft Visual Studio 2010, and it loads and functions perfectly in Python. Here's the code and output:
Test DLL loading/calling code:
__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
Expected console output:
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
I'm really stuck here after spending days on this. Any guidance or troubleshooting directions would be greatly appreciated.
内容的提问来源于stack exchange,提问作者ilbi




