Today, we just made a static DLL to be linked to the MFC, that is, "use MFC in a static library", which is used to display a form. Then it can be executed in an MFCProgramCall this DLL file in a.exe to display the form normally. However, after a small change, the DLL is changed to the DLL dynamically connected to the MFC, that is, "use MFC in a shared DLL later, then the DLL is called in a.exe, and the result form is not displayed. After careful analysis, the reasons are as follows:
DLL dynamically connected to MFC. You can use its executable program to connect to both the mfc dll and any MFC extension DLL. When the mfcshared library is used, mfcloads the resource template using the resource handle of the main application program (a.exe in this example. In this way, when resources with the same ID exist in the DLL and application, the system always uses the resources of the application by default, so the correct resources cannot be obtained. Therefore, when sharing the mfc dll rule DLL, We must switch the module so that MFC can find the correct resource template.
There are three methods:
1. Automatic Switch in DLL. Use afx_manage_state (afxgetstaticmodulestate () in the DLL display form interface function, as shown below:
Void showdlg (void)
{
Afx_manage_state (afxgetstaticmodulestate ());
Cdialog1 DLG;
DLG. domodal ();
}
2. manually switch in DLL, as shown below:
Void showdlg (void)
{
Hinstance save_hinstance = afxgetresourcehandle ();
Afxsetresourcehandle (theapp. m_hinstance); // switch to DLL
Cdialog1 DLG;
DLG. domodal ();
Afxsetresourcehandle (save_hinstance); // restores to an executable program
}
3. Free switch in the Application
Does not make any changes in the DLL, as shown below:
Void showdlg (void)
{
Cdialog1 DLG;
DLG. domodal ();
}
Make the following changes to the place where the DLL is called in the application:
Hinstance Hexe = getmodulehandle (null); // obtain the EXE module handle
Hinstance hdll = getmodulehandle ("1.dll"); // obtain the DLL module handle
Afxsetresourcehandle (hdll); // switch to DLL
Showdlg (); // call the showdlg () function in the DLL.
Afxsetresourcehandle (Hexe); // restores it to an executable program.