Module switching-resource usage in the MFC rule DLL

Source: Internet
Author: User

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.

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.