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.