Python calls C++ DLL library------OSError: [WinError 126] The specified module cannot be found.

Python calls C++ DLL library

Solve the problem of OSError: [WinError 126] in Python that the specified module cannot be found .

1. Question:

  In the process of Python development, it is likely to encounter the situation of calling the C++ DLL library (in order to improve efficiency ), then you need to use the ctypes package to call

  • ctypes.cdll.LoadLibrary(“path”)或者
  • ctypes.CDLL(“path”),

  Then you may encounter the following problem: The general meaning is that the specified library file cannot be found, but this path is like this.

Insert picture description here

Traceback (most recent call last):
  File "E:/WorkSpace/BT/DemoProject/main.py", line 28, in <module>
    LoadDll()
  File "E:/WorkSpace/BT/DemoProject/main.py", line 13, in LoadDll
    lib = ctypes.CDLL("E:\WorkSpace\BT\DemoProject\HelloWorld.dll")
  File "C:\Users\guoqing.zhang\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] 找不到指定的模块。

Second, the solution I tried

  • When writing the path, I tried single quotes , double quotes , slashes and backslashes , but none of them caused this.
  • It was suspected that the python installation package was missing, and then I went to install the ctypes library. After installing a lot of them, the problem was not solved.

3. Online solutions:

  • Said it is a bit problem , python is 64-bit and the generated DLL library file is 32-bit, which does not match.
  • Said it is an encoding problem , it is the confusion between ANSI and Unicode.
  • It is also said that there is a problem with the calling method . There are two methods of stdcall and cdecl that cause the problem.
  • Try to add the current dll library path : os.chdir("E:\WorkSpace\BT\tool\Download\res")
  • It may be that the DLL depends on other library files , and other library files need to be added to the current directory. (How to find the dependent dll library online search)

Fourth, the final solution:

  • For the dependency problem of the Dll library , you need to find out the other library files that the DLL library you use depends on, and put them in the project directory.

  • If you also create a file in the project directory to put the DLL library file, you need to add the folder path , through this function: os.chdir("E:\WorkSpace\BT\tool\Download\res")
    Insert picture description here

  • Look for other DLL library files that the library file depends on. It is recommended to use Dependency Walker . You can find other library files that the library you use depends on, and then go to Windows/system32 to find other library files that you depend on.
    Download address or contact the author's email
    Insert picture description here

  • If the digits do not match, the error reported is not 126, but 193 . In this case, just re-use x64 to recompile and generate a 64-bit library file.

Traceback (most recent call last):
  File "E:/WorkSpace/BT/DemoProject/main.py", line 28, in <module>
    LoadDll()
  File "E:/WorkSpace/BT/DemoProject/main.py", line 13, in LoadDll
    lib = ctypes.CDLL("E:\WorkSpace\BT\DemoProject\HelloWorld.dll")
  File "C:\Users\guoqing.zhang\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 不是有效的 Win32 应用程序。

Insert picture description here
Insert picture description here

  • If there is a function call problem (stdcall and cdecl), it should not be that the module cannot be found, but the corresponding function cannot be found. C++ overloading and C does not support overloading and extern "C" are used here. For details, please refer to the author's article. In this case, it is necessary to add extern "C" modification in the function name declaration in the dll library .
    Static library (lib) and dynamic library (DLL)-understanding of function overloading

When I was debugging, the author tried it and found this situation.

DLL library program: Compile directly to form a DLL library.

#ifndef PCH_H
#define PCH_H

#include "framework.h"
#include "iostream"

#define LIB_API  __declspec(dllexport)

LIB_API void HelloWorld();
LIB_API int  Sub(int a, int b);

#endif 


#include "pch.h"
using namespace std;

LIB_API void HelloWorld()
{
    
    
	cout << "HelloWorld" << endl;
}


LIB_API int  Sub(int a, int b)
{
    
    
	return ((a)-(b));
}

Python calling program:

def LoadDll():
    lib = ctypes.CDLL("E:\WorkSpace\BT\DemoProject\HelloWorld.dll")
    lib.HelloWorld()
    lib.Sub.argtypes = (c_int,c_int)
    lib.Sub.restype = c_int
    print(lib.Sub(3,1))
    return lib.Sub(3,1)

Error:
Insert picture description here

Traceback (most recent call last):
  File "E:/WorkSpace/BT/DemoProject/main.py", line 28, in <module>
    LoadDll()
  File "E:/WorkSpace/BT/DemoProject/main.py", line 14, in LoadDll
    lib.HelloWorld()
  File "C:\Users\guoqing.zhang\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 377, in __getattr__
    func = self.__getitem__(name)
  File "C:\Users\guoqing.zhang\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 382, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'HelloWorld' not found

Guess you like

Origin blog.csdn.net/qq_34430371/article/details/112261682