DLL dynamic link library Programming Primer III: MFC rules DLL (TOP)

Source: Internet
Author: User

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
    1. BOOL cregulardllsocketapp::initinstance ()
    2. {
    3. if (! AfxSocketInit ())
    4. {
    5. AfxMessageBox (idp_sockets_init_failed);
    6. return FALSE;
    7. }
    8. return TRUE;
    9. }

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
  1. Regulardll.h:main header file for the Regulardll DLL
  2. #if!defined (afx_regulardll_h__3e9cb22b_588b_4388_b778_b3416adb79b3__included_)
  3. #define AFX_REGULARDLL_H__3E9CB22B_588B_4388_B778_B3416ADB79B3__INCLUDED_
  4. #if _msc_ver >
  5. #pragma once
  6. #endif//_msc_ver >
  7. #ifndef __afxwin_h__
  8. #error include ' stdafx.h ' before including this file for PCH
  9. #endif
  10. #include "resource.h"//main symbols
  11. Class Cregulardllapp: Public CWinApp
  12. {
  13. Public
  14. Cregulardllapp ();
  15. Declare_message_map ()
  16. };
  17. #endif
  18. RegularDll.cpp:Defines the initialization routines for the DLL.
  19. #include "stdafx.h"
  20. #include "RegularDll.h"
  21. #ifdef _DEBUG
  22. #define NEW Debug_new
  23. #undef this_file
  24. Static char this_file[] = __file__;
  25. #endif
  26. Begin_message_map (Cregulardllapp, CWINAPP)
  27. End_message_map ()
  28. /////////////////////////////////////////////////////////////////////////////
  29. Cregulardllapp Construction
  30. Cregulardllapp::cregulardllapp ()
  31. {
  32. }
  33. /////////////////////////////////////////////////////////////////////////////
  34. The one and only Cregulardllapp object
  35. 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
    1. {{no_dependencies}}
    2. Microsoft Developer Studio generated include file.    
    3. Used by Regulardll.rc
    4. //   
    5. #define Idd_dll_dialog
    6. #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
    1. #include "StdAfx.h"
    2. #include "DllDialog.h"
    3. extern "C" __declspec (dllexport) void Showdlg (void)
    4. {
    5. Cdlldialog Dlldialog;
    6. Dlldialog.domodal ();
    7. }

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
  1. void Cregulardllcalldlg::oncalldllbutton ()
  2. {
  3. typedef VOID (*lpfun) (void);
  4. hinstance hDLL; //dll handle
  5. hDLL = LoadLibrary ("RegularDll.dll");
  6. if (null==hdll)
  7. {
  8. MessageBox ("DLL failed to load");
  9. }
  10. Lpfun Addfun; //function pointer
  11. Lpfun Pshowdlg = (lpfun) GetProcAddress (hDLL,"Showdlg");
  12. if (NULL==PSHOWDLG)
  13. {
  14. MessageBox ("function search failed in DLL");
  15. }
  16. Pshowdlg ();
  17. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.