Switch of the resource module handle

Source: Internet
Author: User
Tags bool

The root cause of this problem is that applications that share MFC DLL (or MFC extension DLLs) with the MFC rule DLLs always use EXE resources by default, and we have to switch the handle of the resource module, which is implemented in three ways:

Method one is used in DLL interface functions:

Afx_manage_state (AfxGetStaticModuleState ());

Let's change the interface function Showdlg in the DLL to:

void Showdlg (void)

{

Method 1: Change at the beginning of the function, restore at the end of the function

Afx_manage_state (AfxGetStaticModuleState ()); module state switching as the first//clause of an interface function

Afx_manage_state (AfxGetStaticModuleState ());

CDialog dlg (Idd_dll_dialog);//Open the dialog box with ID 2000

Dlg. DoModal ();

}

This time we click on the "Call DLL" button in the EXE program, which pops up a dialog box in the DLL as shown in Figure 13. Hey, pop up the right dialog resource.

AfxGetStaticModuleState is a function whose prototype is:

afx_module_state* AFXAPI afxgetstaticmodulestate ();

The function's function is to create an instance of the Afx_module_state class (module global data, which is the module state) on the stack (which means its scope is local), set it, and return its pointer pmodulestate.

The prototype of the Afx_module_state class is as follows:

Afx_module_state (global data for a MODULE)

Class Afx_module_state:public CNoTrackObject

{

Public

#ifdef _afxdll

Afx_module_state (BOOL bdll, WNDPROC pfnafxwndproc, DWORD dwversion);

Afx_module_state (BOOL bdll, WNDPROC pfnafxwndproc, DWORD dwversion,bool bsystem);

#else

Afx_module_state (BOOL Bdll);

#endif

~afx_module_state ();

cwinapp* M_pcurrentwinapp;

HINSTANCE M_hcurrentinstancehandle;

HINSTANCE M_hcurrentresourcehandle;

LPCTSTR M_lpszcurrentappname;

...//omit the following part

}

The Afx_module_state class uses its constructors and destructors for the storage module state scene and the recovery site, similar to the call instruction in the assembly to save and restore the PC pointer and SP Register, Interrupt Service program interrupts the field stack and restore as well as the operating system thread scheduling task control block save and restore.

Many seemingly irrelevant points of knowledge are surprisingly similar.

Afx_manage_state is a macro whose prototype is:

Afx_manage_state (afx_module_state* pModuleState)

This macro is used to set the pmodulestate to the current active module state. When you leave the scope of the macro (and leave the scope of the object that Pmodulestate points to on the stack), the previous module state will be restored by the Afx_module_state's destructor.

Method Two is used in DLL interface functions:

AfxGetResourceHandle ();

AfxSetResourceHandle (hinstance xxx);

AfxGetResourceHandle is used to get the current resource module handle, while AfxSetResourceHandle is used to set the resource module handle that the program is currently using.

Let's change the interface function Showdlg in the DLL to:

void Showdlg (void)

{

State change of Method 2

HInstance save_hinstance = AfxGetResourceHandle ();

AfxSetResourceHandle (theapp.m_hinstance);

CDialog dlg (Idd_dll_dialog);//Open the dialog box with ID 2000

Dlg. DoModal ();

State Restore of Method 2

AfxSetResourceHandle (save_hinstance);

}

With the reasonable change of AfxGetResourceHandle and AfxSetResourceHandle, we have the flexibility to set the resource module handle of the program, and a method can only recover the module handle when the DLL interface function exits. Method Two is different, if SHOWDLG is changed to:

extern Cshareddllapp Theapp; Need to declare theapp external global variables

void Showdlg (void)

{

State change of Method 2

HInstance save_hinstance = AfxGetResourceHandle ();

AfxSetResourceHandle (theapp.m_hinstance);

CDialog dlg (Idd_dll_dialog);//Open the dialog box with ID 2000

Dlg. DoModal ();

State Restore of Method 2

AfxSetResourceHandle (save_hinstance);

After you use Method 2, the following action will be made for the application's resources

CDialog Dlg1 (Idd_dll_dialog); Open the dialog box with ID 2000

Dlg1. DoModal ();

}

Clicking on the Call DLL button in the main Application dialog box will see two dialogs, one after the other, in the dialog box in the DLL (Figure 13) and in the EXE (Figure 14).

Method Three is switched by the application itself

The switching of resource modules can be done by the application itself, in addition to the DLL interface functions.

Now let's change the interface function in the DLL to the simplest:

void Showdlg (void)

{

CDialog dlg (Idd_dll_dialog); Open the dialog box with ID 2000

Dlg. DoModal ();

}

Instead, change the Oncalldllbutton function of the application to:

void Cshareddllcalldlg::oncalldllbutton ()

{

Method 3: State switching by the application itself

Get EXE module handle

HInstance exe_hinstance = GetModuleHandle (NULL);

or hinstance exe_hinstance = AfxGetResourceHandle ();

Get DLL module handle

HInstance dll_hinstance = GetModuleHandle ("SharedDll.dll");

AfxSetResourceHandle (dll_hinstance); Toggle Status

Showdlg (); A dialog box for the DLL appears

AfxSetResourceHandle (exe_hinstance); Recovery status

Resource module is restored before calling Showdlg

Showdlg (); The EXE's dialog box is now displayed

}

The Win32 function in method three GetModuleHandle can get the module handle of a DLL based on the file name of the DLL. If you need to get a handle to the EXE module, you should call GetModuleHandle with a null parameter.

The difference between method three and method two lies in the method of using AfxGetResourceHandle and afxsetresourcehandle to switch the handle of the resource module in the application. Similarly, clicking on the Call DLL button in the main Application dialog box will also see two dialogs, one after the other, in the dialog box in the DLL (Figure 13) and in the EXE (Figure 14).

In the next section we will analyze and illustrate the MFC extension DLLs in detail, and you are welcome to continue to follow this series.

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.