The Non-MFC DLLs are explained in the previous section, and this section describes how to create an MFC rule DLL and how to use the MFC rule DLLs.
I. Overview of MFC Rules DLLs
The concept of the MFC rules DLL is embodied in two ways:
(1) It is MFC's
"is MFC" means that MFC can be used inside this DLL;
(2) It's a rule.
"is regular" means that it differs from the MFC extension DLL, although MFC can be used inside the MFC rule DLL, but its interface with the application cannot be MFC. The MFC extension DLL and the application interface can be MFC, and you can export a derived class of an MFC class from an MFC extension DLL.
Regular DLLs can be called by applications written by all languages that support DLL technology, including applications that use MFC, of course. In this dynamic connection library, a class that inherits from CWinApp is included, and the DllMain function is automatically provided by MFC.
The Regular DLLs are divided into two categories:
(1) A regular DLL that is statically linked to MFC
Static links to MFC's regular DLLs and MFC libraries (including MFC extension DLLs) are statically linked, and the code for the MFC library is generated directly in the. dll file. When calling this DLL's interface, MFC uses the DLL's resources. Therefore, there is no need to switch the module state in a regular DLL that is statically linked to MFC.
A rule DLL generated using this method has a large program, and may contain duplicate code.
(2) Dynamically linked to MFC's regular DLLs
A regular DLL that is dynamically linked to MFC can dynamically link to an MFC DLL and any MFC extension DLLs at the same time as the executable file that uses it. When using the MFC shared library, by default, MFC uses the resource handle of the main application to load the resource template. This way, the system may not be able to obtain the correct resources when a resource with the same ID exists in the DLL and the application (that is, the so-called resource duplication problem). Therefore, for a regular DLL that shares an MFC DLL, we must switch the module so that MFC can find the correct resource template.
We can set the MFC rules in Visual C + + DLLs to be statically linked to an MFC DLL or dynamically linked to an MFC DLL. 1, select the General menu or options for Visual C + + project-Settings, and then set it up in Microsoft Foundation classes.
Figure 1 Setting up a dynamic/static link to an MFC DLL
Second, the MFC rules DLL creation
Let's step through the process of creating an MFC regular DLL using the MFC Wizard, first create a new project,2, and choose the type of Project as MFC AppWizard (DLL). Click OK to enter the dialog box shown in 3.
Figure 2 Creation of an MFC DLL project
Figure 3 Creation options for MFC DLLs
Figure 3 shows the category of the MFC DLL in the 1 area of the dialog box.
Zone 2 Select whether to support Automation (automation) technology, Automation allows users to manipulate another application or component in one application. For example, we can take advantage of Microsoft Word or Microsoft Excel tools in the application, which is transparent to the user. Automation technology can greatly simplify and accelerate the development of applications.
Zone 3 Select whether Windows Sockets is supported, and when this item is selected, the application can communicate on the TCP/IP network. The InitInstance member function of the CWinApp derived class initializes support for the communication side, and the StdAfx.h file in the project automatically include the <AfxSock.h> header file.
The InitInstance member function after adding socket communication support is as follows:
C + + code
- BOOL cregulardllsocketapp::initinstance ()
- {
- if (! AfxSocketInit ())
- {
- AfxMessageBox (idp_sockets_init_failed);
- return FALSE;
- }
- return TRUE;
- }
Zone 4 Select whether the MFC Wizard automatically adds comments to the source code, generally we select "Yes,please".
a City simple MFC rule DLL
An example of this DLL (which belongs to a regular DLL statically linked to MFC) provides a dialog box shown in 4.
Figure 4 MFC Rule DLL Example
The dialog box is added in the DLL in the same way as in an MFC application.
When you click on the Hello button of the dialog box in the DLL shown in Figure 4, a "Hello,pconline User" dialog box is clicked, and the following is the relevant file and source code, which removes most of the comments automatically generated by the MFC Wizard:
First set of files: Declaration and implementation of CWinApp inheriting classes
C + + code
- Regulardll.h:main header file for the Regulardll DLL
- #if!defined (afx_regulardll_h__3e9cb22b_588b_4388_b778_b3416adb79b3__included_)
- #define AFX_REGULARDLL_H__3E9CB22B_588B_4388_B778_B3416ADB79B3__INCLUDED_
- #if _msc_ver >
- #pragma once
- #endif//_msc_ver >
- #ifndef __afxwin_h__
- #error include ' stdafx.h ' before including this file for PCH
- #endif
- #include "resource.h"//main symbols
- Class Cregulardllapp: Public CWinApp
- {
- Public
- Cregulardllapp ();
- Declare_message_map ()
- };
- #endif
- RegularDll.cpp:Defines the initialization routines for the DLL.
- #include "stdafx.h"
- #include "RegularDll.h"
- #ifdef _DEBUG
- #define NEW Debug_new
- #undef this_file
- Static char this_file[] = __file__;
- #endif
- Begin_message_map (Cregulardllapp, CWINAPP)
- End_message_map ()
- /////////////////////////////////////////////////////////////////////////////
- Cregulardllapp Construction
- Cregulardllapp::cregulardllapp ()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- The one and only Cregulardllapp object
- Cregulardllapp Theapp;
Analysis:
A class Cregulardllapp that inherits from CWinApp is defined in this set of files, and an instance of Theapp is defined at the same time. At first glance, you'll think of it as an MFC application, because the MFC application also contains a class that adds "App" to the class name after the project name (and inherits from the CWinApp Class), and also defines a global instance of this class, Theapp.
We know that in an MFC application CWinApp supersedes the WinMain in the SDK program, and the work done by the SDK WinMain is done by the three functions of CWinApp:
Virtual BOOL initapplication ();
Virtual BOOL InitInstance ();
Virtual BOOL Run (); The "source of living Water" in the legendary MFC program
However, the MFC rule DLL is not an MFC application, and the class that inherits from CWinApp does not contain a message loop. This is because the MFC rule DLL does not contain the CWinApp::Run mechanism, and the main message pump is still owned by the application. If the DLL generates a modeless dialog box or has its own main frame window, the application's main message pump must call the function exported from the DLL to call the PreTranslateMessage member function.
In addition, the MFC rule DLL, like in an MFC application, requires that the initialization of elements in all DLLs be put into the InitInstance member function.
Second set of Files: custom dialog box class declaration and implementation
Analysis:
This part of the programming is no different from the general application at all, so we can still use the MFC Class Wizard to automatically add events to the controls on the dialog box. The MFC Class wizard will still generate a message-map macro similar to on_bn_clicked (Idc_hello_button, Onhellobutton).
Third set of files: resource files in DLLs
C + + code
- {{no_dependencies}}
- Microsoft Developer Studio generated include file.
- Used by Regulardll.rc
- //
- #define Idd_dll_dialog
- #define Idc_hello_button 1000
Analysis:
The use of resources in MFC regular DLLs is no different from using resources in MFC applications, so we can use Visual C + + resource editing tools to make resource additions, deletions, and property changes.
Fourth set of files: MFC rules DLL interface functions
C + + code
- #include "StdAfx.h"
- #include "DllDialog.h"
- extern "C" __declspec (dllexport) void Showdlg (void)
- {
- Cdlldialog Dlldialog;
- Dlldialog.domodal ();
- }
Analysis:
This interface does not use MFC, but it can call the MFC extension class Cdlldialog function, which embodies the "rules" of the class.
As with non-MFC DLLs, we can export the interfaces in the MFC rules DLL using the __declspec (dllexport) declaration or the way that is derived in. def.
Iv. calls to MFC rules DLLs
I have written 5 of the dialog MFC program to invoke the third section of the MFC Rules DLL, the dialog box on this program click on the "Call DLL" button when the third section of the MFC Rules DLL dialog box.
Figure 5 An example of an MFC rule DLL invocation
The following is the message handler function for the call DLL button click event:
C + + code
- void Cregulardllcalldlg::oncalldllbutton ()
- {
- typedef VOID (*lpfun) (void);
- hinstance hDLL; //dll handle
- hDLL = LoadLibrary ("RegularDll.dll");
- if (null==hdll)
- {
- MessageBox ("DLL failed to load");
- }
- Lpfun Addfun; //function pointer
- Lpfun Pshowdlg = (lpfun) GetProcAddress (hDLL,"Showdlg");
- if (NULL==PSHOWDLG)
- {
- MessageBox ("function search failed in DLL");
- }
- Pshowdlg ();
- }
The above example shows the way the call is displayed, as you can see, the way it is called is no different from the way the non-MFC DLLs are called in the previous section.
Original address: http://www.jizhuomi.com/software/297.html
DLL dynamic link library Programming Primer III: MFC rules DLL (TOP)