The main program code below
BOOL CtestDlg: : OnInitDialog ()
{
CDialog: : OnInitDialog ();
PFunGetUI typedef int (*) (void * & amp; P_dlg, unsigned int& I_dlg_id);
HINSTANCE hExeIns=AfxGetInstanceHandle ();
HINSTANCE HINSTANCE=LoadLibrary (L "XXX. DLL");
PFunGetUI getUI=(pFunGetUI) GetProcAddress call (hInstance, "getUI");
AfxSetResourceHandle (hInstance);
Unsigned int dlg_id=0;
CDialog * pDlg=0;
GetUI ((void * & amp;) PDlg dlg_id);
If (pDlg - & gt; Create (dlg_id)==FALSE) //this step a success, but the main program this pointer has changed, as if out of memory the
return FALSE;
AfxSetResourceHandle (hExeIns);
//behind if you use this pointer will be abnormal
}
DLL export functions to achieve the following
CMyUI * myUI=0;//global variables, based on a CDialog
DLL_EXPORT_API int GetUI (void * & amp; P_dlg, unsigned int & amp; I_dlg_id)
{
AFX_MANAGE_STATE (AfxGetStaticModuleState ());
MyUI=new CMyUI;
//pointer dialog:
P_dlg=myUI;
//dialog template ID:
I_dlg_id=IDD_MYUI;
return 0;
}
CodePudding user response:
This is not a special case, I try to write a lot of MFC DLL test, will be in this way, if the call will no longer do other operations, the Create page can display properly, but as long as a use this pointer, or access to any member variable of the main program, will be abnormalCodePudding user response:
Read through the DLL in the MSDN development related chapters,CodePudding user response:
Zhao four teachers, the reply is too general, suspected I yoCodePudding user response:
Environment, the upCodePudding user response:
You in OnInitDialog () using AfxSetResourceHandle (hInstance); Actually, this function is invalid, because the dialog resource is already loaded came in, so that is can't change the resources, is a must to replace in haven't create window is loaded, you can in the InitInstance (), if you want to display the dialog box in a DLL, can do processing in a DLLCodePudding user response:
Model has been limited, can only be created in the main dialog window, is such a demand, the demand for the main program can load the DLL, but can't see the source doesn't know loading logic, since the program can show, there was a way to realizeCodePudding user response:
In the main program InitInstance try to load and still have the same problemCodePudding user response:
DLL in the dialog box operation should be AFX_MANAGE_STATE (AfxGetStaticModuleState ()); Within the scope of theTherefore recommended for the Create also on DLL
CodePudding user response:
That's strange, the customer give program is how to implement, okay, I can only try to Create within the DLLCodePudding user response: