Implementing methods for exporting classes and dialog boxes in MFC extension DLLs _c language

Source: Internet
Author: User

This article describes the MFC extension DLL to export Classes and dialog box implementation methods, shared for everyone to reference. The implementation methods are as follows:

In general, if you want to write modular software, you must have a certain understanding of dynamic link library (DLL), I this time in the modification of the previous software, decided to reuse the class and dialog box to do the DLL, the following from a simple example, how to implement the MFC extension DLL Export classes and dialog boxes.

The results of the program run as shown in the following illustration:

First, create an MFC extension DLL

Steps:

Run Visual Studio 6.0->file->new...->projects;
Select MFC AppWizard (DLL), enter Exportclass under project name on the right, and click Next.
Select the MFC Extension DLL (using share MFC DLL) click Finish;
This completes the framework for establishing the extension DLL, and then builds the output class:
Insert-> New Class ... In the Open dialog box, "Class Type" selects "Generic Class", and Class Name enters
Cdogclass (Of course you can use other names, I like puppies, all with it, under "Base Class" input CObject, and then "OK";
Add two test variables int m_nnum for the class; CString M_sdata; The complete class information is as follows:

DogClass.h header file:

Copy Code code as follows:
Class Cdogclass:public CObject
{
Private:
int m_nnum;
CString M_sdata;
Public
int Showdlg ();
void Init ();
Cdogclass ();
Virtual ~cdogclass ();
void setnum (int num) {m_nnum = num;};
int Getnum () {return m_nnum;};
void SetData (CString data) {m_sdata = data;};
CString GetData () {return m_sdata;};
};

DogClass.cpp implementation file:
Copy Code code as follows:
Cdogclass::cdogclass ()
{
Init ();
}
Cdogclass::~cdogclass ()
{}
void Cdogclass::init ()
{
M_nnum = 0;
M_sdata = "";
}

Edit the DogClass.h header file to modify the following line; class Afx_ext_class Cdogclass:public CObject
The above completion class establishment, below establishes the Output dialog box;insert-> resouce ... Create a new dialog box, add two edit boxes Idc_edit1,idc_edit2, and then create the Cdlgdog dialog class and add the int m_nnum and CString m_sdata variables to the two edit box.
Add #include "DlgDog.h" under DogClass.cpp's #include "DogClass.h"
In the Dogclass class, add the int Showdlg () function to invoke the dialog box and add the following code to the int Cdogclass::showdlg ():
Copy Code code as follows:
Cdlgdog Dlg;
Dlg.m_nnum = Getnum ();
Dlg.m_sdata = GetData ();
if (Idok==dlg. DoModal ())
{
Setnum (Dlg.m_nnum);
SetData (Dlg.m_sdata);
return 1;
}
return 0;

Then compile the DLL, you will find a few errors, do not fear, to solve, first of all, remove the #include in the DlgDog.cpp "/Add additional includes here" line; second, add include in Stdax.h Resource.h ", then compile, OK.

Second, the establishment of DLL test procedures

Steps:

Create a new MFC application based on the dialog box, add a button idc_button1, and add the following code to the button event:

Copy Code code as follows:
void Ctestexportclassdlg::onbutton1 ()
{
Cdogclass Dog;
Dog. Init ();
if (dog. Showdlg ()!=0)
{
CString str;
Str. Format ("Num is:%d Data is:%s") dog. Getnum (), dog. GetData ());
AfxMessageBox (str);
}
}

Copy DogClass.h and ExportClass.lib files from the Exportclass file directory to the test program directory;

Add the #include "DogClass.h" line to the TestExportClassDlg.cpp, adding ExportClass.lib to the connector's import library, project->settings under vs setting For select all Configuration, and then add ExportClass.lib to the page below Object/library Conttrol.

Don't forget to copy the ExportClass.dll to the system directory or the current application directory, and then compile the OK.

Add:

When debugging in a vc6.0 environment, the steps above insert-> resouce ... Modified to: Insert-->form

I hope this article will help you with your MFC programming.

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.