I'm trying to design an external DLL for my python program. Now I could use the C++ and Visual Studio 2010 to produce a file with a postfix of ".pyd". If the .pyd is not attached with other .dll files produced by C++, this python library could work well.
However, now I need to design a .pyd file with such a struction:
A.pyd -> 1.dll
-> 2.dll
in which the files 1,2 are C libraries. The functions in these libraries are called when producing A.pyd.
Although this .pyd file could be produced without errors by VS 2010, it could not work in python 3.6. The error report is as follow:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed, could not find the target program.
Even when the referred 1.dll and 2.dll are stored in the same folder that contains A.pyd, this error still exists. I wonder how could I enable the python-C++ library to call the functions in these dynamic link libraries based on C.
OK! Now I have found the correct way to perform this operation!
Wrap the DLL
At first, if the dll (I call it
dll-A) that you need to call has not exported any function (you could usedumpbinin VS command to check the exported functions), you need to wrap the originaldll-A. Use the implicit call to includedll-A. If there is a function declared in the original .h/.lib, like this:Then you need to create a new dll project and wrap the above function by this:
.h
.cpp
Certainly, if the
dumpbinshows thatdll-Ahave avaliable exported functions, you could skip this step.After exporting this dll (I call it
dll-B), you will get 'dll-B' and its depending files (includingdll-Aanddll-A's depending files).Write the .pyd file
Use the explicit call to refer
dll-B. When using this method, you should not include any lib/.h files, becausedll-Bitself could provide you with enough interfaces. You could load the dll by this method:.h
.cpp (part of a function named
Testdllwhen you write the .pyd project)After build your own .pyd, you could get your python database (I call it
pyd-C).Link the .pyd with Python
In python project, you could test this file by this method:
.py
Then you can find that the function is performed well.
Noted that your .pyd should be in the same folder with where .py is. Because you have set
dll-Binlibdll/dllB.dll,dll-Bshould be put in the folder namedlibdll. However, becausedll-Bcallsdll-Aand other depending dlls implicitly,dll-Aand other files should be in your workspace folder, i.e. the same folder with where .py is.In short, you need to enter the workspace folder, and the folder formation is as follow: