Production and use of 27-function VS2017 dynamic library

Production and use of 27-function VS2017 dynamic library

Reward:
here is the official website of the tool for whether the dynamic library successfully exported the function. Pay attention when downloading. See if there are Debug and XXX versions on the top of your VS head. If you are x86, download x86 and x64 if you download x64.
http://www.dependencywalker.com/

Preface: In fact, the dynamic library generation is exactly the same as the previous static library. It's just that the last one is using VS2013.

1
Click on the Windows desktop wizard. It is not recommended to directly generate the DLL option. There are many files and errors are prone.
Insert picture description here

2
Select Dynamic Link Library - empty project.
Insert picture description here

3
Create the corresponding .h and .cpp files, and input them according to the following method, which is indispensable. The comments are on the top.
1) If _declspec(dllexport) is not written, the dll generated is actually not imported into the function, and can be viewed with the dependency software. Simply drag the generated dll file into it, the software is very small and very practical.
2) Extern "C" means that the user guarantees that the name of the imported dll dynamic library is the same as the name of the source code. The name of the imported function is different from the name of the generated function when it is displayed in the call, because the generated function name has been compiled and processed by C++. The display call of the dynamic library needs to use the related functions under <windows.h>, and use the function pointer to manipulate the first address of the returned function to call the function, which is not written here.
The implicit call can be written directly in the main function, but VS2017 doesn't write it as if it is implicit call by default, because I didn't write it here and it was called normally.

#pragma comment(lib,"xxx.lib")//隐式调用动态库。参数2为路径

Insert picture description here
Insert picture description here

4
The results generated in Debug above are as follows, there must be two, only one indicates that there is an error in your function, check it yourself. MyDll1.lib is not a real static library, but an entry statement for calling dynamic library functions.
Insert picture description here

5
Generate a .cpp file with the main function as usual.

6
Add the header file of the dynamic library. Here I wrote the relative path. If you want to omit the previous things, you can copy the header file of the dynamic library to the current project (ie, the directory with .cpp).
Insert picture description here

7 (Very important) The
above steps only add the header file, but the implementation file is still in the dynamic library, so we need to add it to the project's properties. There are two steps:
1) Project-Properties-VC++ Directory-Library Directory-Add xxx The directory where the .lib is located.
Insert picture description here

2) The
first step is only to add the library directory, but in fact, no specific library files have been added.
Linker-Input-Additional Dependencies-Add all library files used-Application-OK.
This time it is this file. The first step is the library directory. Pay attention to this point.
Insert picture description here
In addition: In fact, the seventh step can be replaced by this method, which is to copy the corresponding library files and dll files to the current project, then right-click the resource file-add existing resources; it can also be successfully called.

8
At this time, we can see that we successfully called the packaged dynamic library. The main difficulty is to add header files and library files, you need to be familiar with the path and the generated directory.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/108171005