If you want to write modular software, you must have a certain understanding of the dynamic link library (DLL). During this period of time when I modified the previous software, I decided to implement repeated classes and dialogs in DLL. The following is a simple example of how to export classes and dialogs in the MFC extension DLL.
1. 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 MFC extension DLL (using share mfc dll) and click "finish ";
The above completes the establishment of the extension DLL framework, and then the establishment of the output class:
Insert-> new class... In the displayed dialog box, select "class type" and enter
Cdogclass (of course you can use other names. I like puppies. All of them use it. Input cobject under "base class" and then "OK ";
Add two Test variables int m_nnum; cstring m_sdata; to the class. The complete class information is as follows:
Dogclass. h header file:
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: Cdogclass: cdogclass () { Init (); } Cdogclass ::~ Cdogclass () {} Void cdogclass: Init () { M_nnum = 0; M_sdata = ""; } |
Edit the dogclass. h header file and modify the following line; Class afx_ext_class cdogclass: Public cobject
The above completes the creation of the class, and the following creates an output dialog box; insert-> resouce... create a dialog box, add two edit boxes idc_edit1 and idc_edit2, create a cdlgdog dialog box class, and add int m_nnum and cstring m_sdata variables to the two edit boxes.
Add # include "dlgdog. H" to # include "dogclass. H" of dogclass. cpp"
Add int showdlg () to the dogclass class. Use the function to call the dialog box and add the following in int cdogclass: showdlg ():Code:
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 and you will find several errors. Do not be afraid. To solve this problem, first remove the dlgdog. # include "\ add additional includes here" in CPP; secondly, in stdax. add include "resource. H ", compile again, OK.
2. Create a DLL TestProgram
Steps:
Create a dialog box-based MFC application, add the idc_button1 button, and add the following code in the button event:
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 the dogclass. h and exportclass. Lib files from the exportclass file directory to the test program directory;
In testexportclassdlg. add # include "dogclass. h. lib, select all configuration under setting for in vs project-> Settings ., then add exportclass to the object/library conttrol in the following page. LIB;
Do not forget to copy exportclass. DLL to the system directory or the current application directory; then compile it and you will be OK.