C ++ calls the. Lib method:
I. There are three methods for implicit link loading:
1. Add the Lib file to the project file list.
Open the file view page in VC, select the project name, right-click the project, and select the "add files to project" menu, in the pop-up file dialog box, select the Lib file to be added to the DLL. Then add the header file of the lib to the function, such as # include ".. \ Lib. H ).
2. Set Project settings of the project to load the DLL lib file.
Open the Project Settings menu of the project, select link, and enter the DLL lib file in the text box under object/library modules, such as you. lib (or the path of the Lib file, including the file name ). Then add the header file of the lib to the function, such as # include ".. \ Lib. H ).
3 passProgramCodeMethod
Add the pre-compiled command # pragma comment (Lib, "*. lib"). The advantage of this method is that the conditional pre-compiled command can be used to link lib files of different versions. In the debug mode, the generated lib file is the debug version, such as regd. Lib. In the release mode, the generated lib file is the release version, such as Regr. Lib. Then add the header file of the lib to the function, such as # include ".. \ Lib. H ).
After the application loads the Lib file of the DLL, it also needs to load the header file (*. h) include it. In this header file, the prototype of the function defined in the DLL is given, and then declare
2. Explicit runtime Link (I am using this method)
Although the implementation of implicit links is relatively simple, except for the required *. *. h file and *. lib files, which only provide *. DLL files cannot be used, but can only be explicitly linked. In this way, you can call an API function to load and detach a DLL to use the memory more effectively. This method is often used when writing large applications. The specific steps for programming this method are as follows:
① Use the Windows API function load library or the afxloadlibrary provided by MFC to mirror the DLL module to the memory space of the process and dynamically load the DLL module.
② Use the getprocaddress function to obtain the pointer to the function in the DLL to be called.
③ When no DLL is used, use the free library function or the afxfreelibrary function to explicitly uninstall the DLL from the address space of the process.
For example, call the DLL file in an application.
-- In an application, you must load the DLL before calling the function in the export table. For example
Create a project test based on the dialog box, place the "LOAD" button in the dialog box, and add the load code first.
1. First add the variable setting code in the header of testdlg. cpp:
// Set the global variable glibsample to store the DLL handle.
Hinstance glibsample = NULL; // if it is defined as the handle type, an error occurs.
// The second variable showme is directed to the DLL
Pointer of the showme () function in the library
Typedef int (* showme) (void );
Showme;
2. Use classwizard to add the DLL Loading Code for the "LOAD" button.
Void ctestdlg: onloadbutton ()
{
// The code to be added is as follows:
If (glibsample! = NULL)
{
Afxmessagebox ("the sample. dll has already been load .");
Return;
}
// Load sample. dll. If no path is added, search for the Windows System directory \ WINDOWS \ system in the three default paths;
// (2) any directory indicated by path in DOS;
// (3) Directory of the program;
Glibsample = loadlibrary ("sample. dll ");
// Return the address of the showme () function in the DLL.
Showme = (showme) getprocaddress (glibsample, "showme ");
Differences between static library lib and dynamic library DLL
1. What is static Connection Library and dynamic link library?
Static and dynamic libraries share code, all commands in lib are directly included in the final generated EXE file. However, if a DLL is used, the DLL does not need to be included in the final EXE file. During execution of the EXE file, the DLL file can be dynamically referenced and detached. Another difference between a static link library and a dynamic link library is that the static Link Library cannot contain any other dynamic or static Link Library, other dynamic or static link libraries can be included in the dynamic link library. The Calling rules of the static Link Library and the static Link Library are compared as follows.
For static link libraries (relatively simple ):
First, the developer of the library is required to use the static Link Library to provide the. h header file and. Lib file of the generated library.
The Declaration format in the. h header file of the generated library is as follows:
Extern "C" function return type function name (parameter table );
In the. cppSource codeThe file is as follows:
# Include ".. \ Lib. H"
# Pragma comment (Lib, ".. \ debug \ libtest. lib ")
// Specify the link to the static database
Second, because the static Link Library contains all commands into the EXE file generated by the calling program. Therefore, if you use a static link library, you do not need to use it to export a function! Don't even ask for it! :)
For Dynamic Link Libraries:
The developer of the library is required to use the dynamic link library to provide the generated. Lib file and. dll file. Alternatively, you can only provide DLL files.
First, we must first notice that the functions in the DLL are divided into two types:
(1) DLL export function, which can be called by a program;
(2) DLL internal functions can only be used in DLL programs, and applications cannot call them.
Therefore, if you want to call a function in a DLL, you must specify in some form or method which function you want to call.