There are two ways that an application can use a DLL: One is an implicit link, the other is an explicit link. Before you use a DLL, you should first know the structure information of the functions in the DLL. Visual c++6.0 provides a small program named Dumpbin.exe in the Vc/bin directory that lets you view the function structure in a DLL file. In addition, the Windows system will follow the search order below to locate dll:1. The directory containing the EXE file, 2. Current working directory of the process, 3. Windows system directory, 4. Windows directory, 5. A series of directories listed in the PATH environment variable.
1. Implicit links
Implicit linking is the loading of DLL files into an application when the program starts executing. It's easy to implement implicit linking, as long as you write the Import function keyword _declspec (dllimport) function name to the appropriate header file for your application. The following example invokes the Min function in the MyDll.dll library through an implicit link. First build a project for Testdll, enter the following code in the DllTest.h, DllTest.cpp file, respectively:
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; A=min (8,10) printf ("The result of comparison is%d/n", a); }
|
Before you create the DllTest.exe file, copy the MyDll.dll and MyDll.lib to the same directory as the current project, or copy it to the Windows system directory. If the DLL is using a def file, delete the keyword extern "C" in the TestDll.h file. The keyword Progam commit in the TestDll.h file is to have the visual c+ compiler link to the MyDll.lib file at link time, and of course, the developer can also not use #pragma comment (lib, "MyDll.lib") statements , while the Object/moduls column directly in the Setting->link page of the project fills the MyDll.lib.
2. Explicit linking
Explicit linking is an application that can load DLL files at any time during execution, or uninstall DLL files at any time, which is not possible by implicit linking, so explicit linking is more flexible and more appropriate for an explanatory language. But it's a bit of a hassle to implement explicit linking. The dynamic-link library's file name, which is the parameter of the two functions mentioned above, is then used GetProcAddress () to get the function to be introduced in the application using LoadLibrary or MFC-provided afxloadlibrary to explicitly redeployment the dynamic-link library. Since then, you can invoke this introduction function just as you would in an application-customized function. You should release the dynamic link library with the afxfreelibrary provided by FreeLibrary or MFC before the application exits. The following is an example of calling the Max function in a 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 dynamic link library MyDll.dll file; max= (Pmax) GetProcAddress (hDLL, "Max"); A=max (5,8); Printf ("The result of comparison is%d/n", a); FreeLibrary (hdll);/uninstall MyDll.dll file; } |
Use the type definition keyword typedef in the example above to define a pointer to the same function prototype in the DLL and then load the DLL into the current application by Loadlibray () and return the handle to the current DLL file, then pass GetProcAddress () function gets the function pointer that is imported into the application, and after the function call completes, the DLL file is unloaded using FreeLibrary (). Before you compile your program, you first copy the DLL files to the directory where the project is located or to the Windows system directory.
You do not need to use the appropriate LIB file when compiling with an explicit linked application. In addition, using the GetProcAddress () function, you can use the Makeintresource () function to directly use the order number that appears in the DLL, such as changing GetProcAddress (hDLL, "Min") to GetProcAddress ( hDLL, Makeintresource (2)) (The Order of Min () in the DLL is 2) so that the function in the calling DLL is fast, but you will remember the function's use sequence number, otherwise an error occurs.