Applications can use DLL in two ways: implicit link and explicit link. Before using the DLL, you must first know the structure information of the function in the DLL. Visual c ++ 6.0 stores a small program named dumpbin.exe in the VC/bindirectory. You can use it to view the function structure in the DLL file. In addition, the Windows system will follow the search order below to locate the DLL: 1. directory that contains the EXE file, 2. current working directory of the Process, 3. windows Directory, 4. windows Directory, 5. A series of directories listed in the PATH environment variable.
1. implicit link
The implicit link loads the DLL file into the application when the program starts execution. It is easy to implement implicit links. You only need to write the name of the imported function keyword _ declspec (dllimport) to the corresponding header file of the application. The following example uses an implicit link to call the min function in the mydll. dll library. First, generate a project named testdll and enter the following code in the dlltest. h and dlltest. cpp files:
// Dlltest. h # Pragma comment (Lib, "mydll. lib ") Extern "C" _ declspec (dllimport) int max (int A, int B ); Extern "C" _ declspec (dllimport) int min (int A, int B ); // Testdll. cpp # Include # Include "dlltest. H" Void main () {Int; A = min (8, 10) Printf ("compare result % d/N", ); } |
Before creating the dlltest.exe file, copy mydll. dll and mydll. lib to the directory where the current project is located, or copy it to the Windows System directory. If the DLL uses the def file, delete the keyword extern "c" in the testdll. h file ". In the testdll. h file, the keyword progam commit is to link to the mydll. Lib file when the Visual C + compiler is linked. Of course, Development Personnel can also not use # pragma comment (Lib, "mydll. lib "), and enter mydll directly in the object/moduls column of the Setting-> link page of the project. lib is supported.
2. explicit link
Explicit link is an application Program DLL files can be loaded at any time during execution, or DLL files can be detached at any time, which is not implemented by implicit links. Therefore, explicit links are more flexible and can be interpreted. Language More suitable. However, implementing explicit links requires a little effort. In the application, call the dynamic link library provided by loadlibrary or the afxloadlibrary provided by MFC explicitly. The file name of the Dynamic Link Library is the parameter of the above two functions, and then use getprocaddress () obtain the function to be introduced. Since then, you can call the introduced function like using a function defined in the application. Before exiting the application, use the afxfreelibrary provided by freelibrary or MFC to release the dynamic link library. The following is an example of calling the max function in DLL through an explicit link.
# Include # Include Void main (void) { Typedef int (* Pmax) (int A, int B ); Typedef int (* pmin) (int A, int B ); Hinstance hdll; Pmax Max Hdll = loadlibrary ("mydll. dll"); // load the dynamic link library mydll. dll file; Max = (Pmax) getprocaddress (hdll, "Max "); A = max (5, 8 ); Printf ("compare result % d/N", ); Freelibrary (hdll); // uninstall the mydll. dll file; } |
In the above example, use the type definition keyword typedef to define the pointer to the same function prototype as the DLL, and then use loadlibray () load the DLL to the current application and return the handle of the current DLL file. Then, use the getprocaddress () function to obtain the function pointer imported to the application. After the function is called, use freelibrary () to uninstall the DLL file. Before compiling a program, copy the DLL file to the project directory or the Windows System directory.
When you use an explicit link to compile an application, you do not need to use the corresponding lib file. In addition, when using the getprocaddress () function, you can use the makeintresource () function to directly use the sequence number in the DLL function, for example, change getprocaddress (hdll, "min") to getprocaddress (hdll, makeintresource (2) (the sequential number of the function min () in the DLL is 2). In this way, the call speed of the function in the DLL is very fast, but remember the sequence number used by the function. Otherwise, an error occurs.
This article is comprehensive and easy to understand Introduction This article introduces the concept of Dynamic Link Library, the creation of dynamic link library, and the link of dynamic link library, and provides a simple and clear example. I believe that after reading this article, ability to create your own dynamic link library and apply it to subsequent Software Development And, of course, readers must be familiar with DLL operations, and they must continue to explore in a large number of practices, Hope This article can serve as a reference.