Use of Dynamic Link Library

Source: Internet
Author: User

The. libfile of the dynamic link database only provides the duplicate table information for the generated. EXE at the link time. The. Lib file ensures that the link passes, and The. h header file ensures that the file passes compilation.

. Exeexecution is also performed by the dig search .dll. when you run .exe, put. dll together.

Two loading methods for Dynamic Link Library:

1. Hidden Link
2. Display Loading

The exported function can be called.

F: \ Program Files \ Microsoft Visual Studio \ vc98 \ bin
Dumpbin

Drag vcvars32.bat to cmd to set the environment variable, and then run dumpbin.

Dumpbin-Exports dll1.dll is used to check which functions are output by the dynamic link library dll1.dll.

Be sure to execute this command in the directory

A hidden link
No function is output without adding
_ Declspec (dllexport)

Note _ declspec (dllexport) is easy to write in upper case and error in case

Output after adding a function

Statement 1 in the test file

Extern int add (int A, int B );
Extern int subtract (int A, int B );

// There is no error during compilation, because the two functions are described as external functions.
// An error occurred while linking. The dynamic link library cannot be found.

Solution: copy the generated dll1.lib to the dlltest directory, and then add dll1.lib through the project --- settings -- Link.
Compile now

Provides input library files without actual code, provides information for links, and relocates table addresses.

However, an error occurs during running.
Analyze the required dynamic link library, search for the dynamic link library on the user's machine, and load

Solution: Let the program find the dynamic link library and copy dll1.dll to the dlltest directory to run it.

Which dynamic link libraries must be depended on by Graph tool dependanaly.exe?

Put dll1.dlland dlltest.exe together to find the analysis, and there is no question mark

// 1. Specify the external Definition
// 2. indicates that the file is read from the. Lib file and is read from the dynamic link library. This method is more efficient.

_ Declspec (dllimport) int add (int A, int B );
_ Declspec (dllimport) int subtract (int A, int B );

I wrote a dynamic link library for use by others. How do others know which functions can be called?
Solution: provide a corresponding header file, provide the declaration of the function prototype, and include some comments.

The header file of the dynamic link library is used (dllimport). It must be provided to the client. for the client, Shi
Input
_ Declspec (dllimport) int add (int A, int B );
_ Declspec (dllimport) int subtract (int A, int B );

Replace _ declspec (dllimport) int add (int A, int B) with the header file # include "C: \ dlltest \ dll1 \ dll1.h );
_ Declspec (dllimport) int subtract (int A, int B); these two declarations
The compilation results are the same.

Provide header files and Dynamic Link Libraries

Modify the header file so that it can serve both the client and its own. cpp.

Header files are not compiled, and CPP files are compiled independently.

Conditional compilation:

In the dynamic link library, only exported functions can be used.
How to export a class?
Class dll1_api point is written in this way, and all data can be exported.
How can I obtain the handle of the function window in the dynamic link library?

Getforegroundwindow () to get the wide port handle for calling the Function


An error occurs when you execute a single. EXE file. You must put the dynamic link library with it.
However, you must remove it during compilation before it can be compiled.

To export only one member function of the class, write the dll1_api separately before the member function of the class.

Generally, the exported member function names are renamed by the compiler. For example, add is changed to add @ yahhhh @ Z.
So it cannot be used by other compilers ,?

Change the name of the C ++ name Adaptation Scheme
Solution:

# Define dllsort API extern "C" _ declspec (dllimport) add extern "c" to uppercase C note: Yes. # define dll1_api extern "C" _ declspec (dllexport) in CPP)

Defect: The member functions of the class cannot be exported. You can only export the global function "add" to ensure that the number of member functions remains unchanged.

This is a C call convention.

2. Load the dynamic link library

Hinstance hinst;
Hinst = loadlibrary (dll1.dll );
Typedef int (* addproc) (int A, int B );

(* Addproc) (int A, int B) as a whole is a new integer type.
Consider (* addproc) as a whole. It is a function that contains two parameters and is replaced with the int type.
Open (* addproc). addproc is a function pointer type, rather than a variable, but can define a variable.

Why display and Load

If there are more than 10 dynamic link libraries, if they are all implicitly loaded, the program will be loaded at startup, and the startup will be slow.

At this time, you only need the. dll file, without the header file or the. Lib file. You just need to put the. dll file in the directory and compile it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.