When the C++ program is released, the dependent library lacks a solution

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

The author is recently studying how to use C++ to call the code of the pcl library, and package the code into a dll for C# to use. The IDE used is VS2017. After a lot of hard work, the packaging was finally successful, and the test passed on this machine. But when I distribute the dll to others, the following error occurs:
System.DllNotFoundException:“无法加载 DLL“PointCloudDll.dll”: 找不到指定的模块

insert image description here
But I obviously put all the Dlls under the same directory of the exe, why does this error still occur?

1. Error analysis

There are generally three reasons for this error.
First, there is a problem with the packaged code.
Second, the number of bits in the dll is inconsistent with that of the target machine. Third
, the packaged dll references other dlls, and these dlls are not included in the distribution.
There is no problem when I test on this machine, so the first point can be ruled out; the dll packaged by myself is 64-bit, and the target machine is also set to 64-bit, so the second point can be ruled out. That problem arises in the third point.
But I didn't refer to other dlls when I packaged it. Yes, we did not use other dlls ourselves, but the computer automatically referenced the related dlls for us. This is related to the dll search path of vs. Generally speaking, the dll search sequence of vs is as follows:
a. The directory where the application is located;

b. System directory. The directory returned by GetSystemDirectory is usually the system disk\Windows\System32;

c. 16-bit system directory. This item is only for forward compatibility processing and can be ignored;

d. Windows directory. The directory returned by GetWindowsDirectory is usually the system disk\Windows;

e. Current directory. The directory returned by GetCurrentDirectory;

f. All directories in the environment variable PATH

Note that the system directory here, whether it is a 32-bit machine or a 64-bit machine, is generally Windows\System32. Some people may say, isn't there a SysWOW64 directory? Shouldn't the 64-bit machine be placed under this directory? Not so, WOW64 is the abbreviation of Windows on Wondows, it exists to be compatible with 32-bit programs. In a 64-bit operating system, 32-bit code and 64-bit code run separately, there are two library folders, System32 and SysWOW64, and even two registry. Generally speaking, according to the rules of 32-bit systems, the folder storing 64-bit dlls should be called System64, but in fact, in order to maintain compatibility, Microsoft still stores 64-bit dlls in the System32 folder, and at the same time, in order to be compatible with 32-bit , set up a SysWOW64 folder.
If you understand this, you should understand why the error was reported. That's because the dll I packaged uses a lot of pcl library code, and I didn't put these related dlls in the directory. But when I run it on the local machine, there is no error. This is because I have configured the environment variables of the pcl library on this machine, so when the encapsulated dll is called, the dll will use the pcl dll. Although I have not put these dlls into the directory, vs can pass the environment variable Find these related dlls, so no error will be reported.

Two, the solution

Now that you know that the problem is caused by the lack of related dlls, the first thing to do is to find out which dlls are missing. Here you need to use a dll analysis tool to analyze which dlls are also referenced in your own packaged dlls. Here we recommend station B up The analysis tool PE analyzer written by the main Hell8999, this is the download link
Through this tool, I can analyze that the dll packaged by myself refers to many other dlls
insert image description here

Among them, the red arrow points to the dll related to pcl, and the green arrow points to the dll of the window system. Generally, normal windows will have these dlls, and the blue one is the dll of the C++ runtime. Here I just need to put the dll of pcl into the directory, and other dlls will be available in most computers. This alone is not enough, because these two pcl dlls may also contain other dlls, such as the pcl_io_release dll, which also references the two dlls pcl_io_ply_release and OpenNI2. Will report the same error. The author did not put these two dlls in, so it took a whole day to find the reason.
Therefore, when encapsulating a dll with a third-party library, you must check the dependencies of each dll. It may be that your program will report an error because of one dll missing.
insert image description here

Summarize

When distributing a dll with a third-party library, remember to distribute the other dlls that the dll depends on together, and don't forget that other dlls may also reference other dlls, just like nesting dolls, layer by layer. No matter how many layers are set, all dlls must be distributed together when distributing!

Guess you like

Origin blog.csdn.net/bookshu6/article/details/125052874