Author: Zhao Yonghua Source: yesky
DLL refers to the dynamic link library, which is an executable binary file that can be called by multiple applications (or even applications written in different languages) at the same time, is a shared library. DLL is a library file that contains a number of functions, classes, or resources. functions and data are stored in a DLL (server) and exported by one or more customers. These customers can be applications or other DLL. Obviously, DLL is a required course for professional VC programmers, because in the development of commercial software, the application is always divided into multiple modular DLL for debugging, linking and maintenance.
The MFC in VC 6.0 supports three types of DLL, including conventional static DLL, conventional dynamiC DLL, and extended DLL. You can use the Appwizard of VC 6.0 to create these three types of DLL: Click the menu item "File> New... ", In the displayed" new "dialog box, activate the" project "column, select" MFC Appwizard (DLL) "in the list box, and enter the project name and path information (1 );
Figure 1
Click "OK" to bring up the "MFC Appwizard-step 1 of 1" dialog box (2 ),
Figure 2
Three Optional circle buttons are displayed: "regular DLL with MFC statically linked", "regular DLL using shared mfc dll", and "MFC extension DLL (using shared mfc dll )". They correspond to three types of DLL in sequence. The difference between conventional static DLL and conventional dynamiC DLL is that the former uses the static Link Library of MFC, And the generated DLL file is long. This method is generally not used, the latter uses the dynamic link library of MFC, And the generated DLL file length is small. All the functions output by dynamic link to the conventional DLL of MFC should start with the following statement (used to correctly switch the status of the MFC module ):
| Afx_manage_state (afxgetstaticmodulestate ()) |
The extension DLL is used to create a derived class of MFC and is called only by applications written in the MFC class library. A feature of conventional DLL (including static and dynamic) is that there is a class inheriting cwinapp in the source file (derived from cwinapp, but there is no message loop ), the exported function is a C ++ class or C ++ member function. Applications that call conventional DLL do not have to be MFC applications. The extension DLL is different from the conventional DLL. It does not have a class Object inherited from cwinapp. The Compiler defaults a DLL entry function dllmain () as the initialization of the DLL.
The following is an example of a DLL program. To this end, you should first create a main program named testdll application, then create a DLL application as a sub-project of the main project, and call the DLL in the main project. Use MFC Appwizard to create a dialog box program, and add a command button (named "Call DLL program") to it. After the program is run, the interface 3 is displayed. Click the button to call the DLL program. The following describes how to compile the DLL program.
Figure 3
1. As shown in figure 2, input the DLL program name "DLL" through the MFC Appwizard (DLL) and click "finish" to generate the basic structure of a DLL program, when running to figure 2, we can select the first button "regular DLL with MFC statically linked" to generate a regular static DLL, you can also select the second button "regular DLL using shared mfc dll" to generate a conventional dynamiC DLL, which is much smaller than the former. When running the DLL, a dialog box with the words "this is my DLL" is displayed. Click "OK" to return to the calling program. Therefore, you need to insert a dialog box resource (the Class Name of the dialog box is cmydlg) in the DLL project. Enter the following code at the beginning of the DLL. cpp program:
Enter the following code after "cdllapp theapp;" in the last line of the DLL. cpp program:
Extern "C" _ declspec (dllexport) int mytest ()
{
Afx_manage_state (afxgetstaticmodulestate ());
Cmydlg DLG;
DLG. domodal ();
Return 0;
} |
Compile the DLL and generate the DLL. dll program.
Then, go to the main program testdll and use classwizard to add the following code to the code snippet of the command button:
Void ctestdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
Typedef int (_ cdecl * Connect )();
Hinstance hinstdll = NULL;
Hinstdll = loadlibrary ("DLL. dll ");
If (hinstdll)
{
Connect proc;
Proc = (CONNECT) getprocaddress (hinstdll, "mytest ");
Int itemp = proc ();
Freelibrary (hinstdll );
}
Else
{
Afxmessagebox ("not found DLL! ");
}
} |
To enable testdll to call the DLL. dll program, you must enable the former to "see" the DLL program. The order in which a Windows program locates the DLL is: 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. Therefore, you need to put the DLL. dll in the relevant directory above, so that the execution of the main program testdll, click the command button to display the following DLL running results, as shown in figure 4.
Figure 4