Output dialog box in MFC extension DLL

Source: Internet
Author: User
It seems easy to export the dialog box from the dynamic link library of MFC extension. You only need to output the corresponding class with afx_ext_class. It may be successful if you use an application and a Dynamic Linked Library. However, if you insert more resources in the application and DLL, you will get a serious error.
The reason is as follows:

Indicates that the ID of a specified resource is used normally. ID is an integer constant defined in the resource editor. Now, assume that you already have a resource (a string) named id_my_text.

Cstring strtext;
Strtext. loadstring (id_my_text );
Afxdump <strtext;

The above code outputs the string to the debugging window. Sometimes you may get an error, and it only happens if the text is in an extended dynamic link library of MFC. The cause of the error is that the application obtains a resource. Because the application and dynamic link library both have a resource file. Different resource IDs may be the same (for example, the VC resource editor determines the starting ID method for each module ).

You may think that the order in which you search for a resource in your application is first for the application and then for your DLL. We need to change the search resource order.

Other articles on this site detail the output dialog box from DLL. However, it can only work in the formal MFC dynamic link library.

I wrote a class (with some changes in the main and dialog box of DLL) that allows you to call your dialog box wherever you like, just like:

Cmyapp: ondlldialog ()
{
Cdlldialog DLG;
DLG. domodal ();
}
I wrote a simple class, set the resource handle of the dynamic link library in the class constructor, and set the previous handle in the destructor.

//////////////////////////////////////// //////////////////////////////////////// /////////////
// File extdllstate. h
//////////////////////////////////////// //////////////////////////////////////// ////////////
# Ifndef _ extdllstate_h __
# DEFINE _ extdllstate_h __

Class cextdllstate
{
Public:
Cextdllstate ();
~ Cextdllstate ();
Protected:
Hinstance m_hinstold;
};

# Endif
//////////////////////////////////////// //////////////////////////////////////// ////////////
File extdllstate. cpp
//////////////////////////////////////// //////////////////////////////////////// ////////////
Cextdllstate: cextdllstate ()
{
M_hinstold = afxgetresourcehandle ();
Afxsetresourcehandle (extensiondll. hmodule );
}

Cextdllstate ::~ Cextdllstate ()
{
Afxsetresourcehandle (m_hinstold );
}
//////////////////////////////////////// //////////////////////////////////////// //////

You may think it is very short but can do a lot of work: copy the above class code to exdllstate. H and extdllstate. CPP, put the two files in the public include directory so that each project can find it. Find the main source file in your DLL and you will see some code similar to the following:

Static afx_extension_module my_dll_namedll = {null, null };
Replace the variable with "extensiondll. Put the following code:

# Include "extdllstate. H"
# Include "extdllstate. cpp"

Find the my_dll_namedll event elsewhere in the file and replace it with extensiondll. An event can only occur in this file, so that the variable is static.

Now, if you want to output a dialog box, enter the source file of the corresponding class and include extdllstate. h. The overload function domodal () (classwizard is recommended). You will see code similar to the following:

Int cmydlldlg: domodal ()
{
// Todo: add your specialized code here and/or call the base class
Return cdialog: domodal ();
}

Use "cextdllstate state;" to replace the todo line, as shown below:

Int cdlldlgdlg: domodal ()
{
Cextdllstate state;
Return cdialog: domodal ();
}

You can use cextdllstate wherever you want to access resources in the MFC extension DLL, and always get the correct results. I hope it will help you.

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.