Recently in the development of a component-based MFC interface, the interface needs to be encapsulated in a dynamic library.
One: Project creation Step 1. Create an MFC DLL project and select "
using MFC in a shared DLL”。 2. Run-time Library select:c/c++--> code Generation---run library "
multithreaded Debug DLLs (/MDD)”。 Two: Define the export interface base class definition export interface class, to export the interface needs to inherit the interface class, and implement the virtual function defined by the interface. As follows: Class Ihpdllwin:p ublic cdialog{public://constructor, ID is window resource idihpdllwin (int id): CDialog (ID) {}//initialization, such as Client network environment virtual void Init (const char* svrip,const int port) = 0; Cleanup, such as client network environment virtual void finit () = 0; Display window virtual int showwin () = 0; Set parameter virtual void SetParam (string p1) = 0; The interface class inherits from CDialog, and all exported interfaces can only be dialog boxes, and if you need to export other types of window classes, modify the window classes that the base class inherits from.
Three: Define the export interface The dynamic library defines an export function that returns a pointer to the base class of the exported interface. This interface can be manipulated externally by obtaining pointers to the exported interface object. Such as:
the following export function is added in the implementation file of the dynamic library's app class, otherwise the dialog box closes the Times assertion error. Define the Export interface extern "C" __declspec (dllexport) ihpdllwin* Getselorgcontrol () {
//remember that the following line of code is required here, otherwise the dialog box does not appear. Afx_manage_state (AfxGetStaticModuleState ()); Ihpdllwin *ph = App_selorgdlg::instance (); return ph;} Four: Calling the dynamic Library in a dynamic manner, call the steps below: 1. Load Dynamic library hinstance m_hdll = LoadLibrary (" SelectOrgControl.dll "); if (NULL = = M_hdll) {MessageBox ("load SelectOrgControl.dll failed");}  2. Get the Export function pointer typedef ihpdllwin* (*hpdllfun) (); Hpdllfun Pshowdlg = (hpdllfun) GetProcAddress (M_hdll, "Getselorgcontrol"); if (NULL==PSHOWDLG) { MessageBox ("function search failed in DLL"); return;} 3. Get export class object pointer ihpdllwin* M_hpwin = Pshowdlg (); Get Export window class pointer , gets the control of the export window. 4. Wizard out window pass parameters M_hpwin->setparam (TheApp.m_UsrName.GetBuffer ()); 5. Call the Initialize function of the export window M_hpwin->init ("127.0.0.1", 25250); 6. Show Export Window M_hpwin->showwin (); V: Unload Dynamic library After use, call the following function to unload the dynamic library: freelibrary (M_HDll);
How to make an MFC DLL with MFC interface