You should also switch resources when exporting functions in an MFC extension DLL.
In a shared (dynamically linked) MFC rule DLL, before each of the exported functions need to switch resources, in the extension DLL also need, because they are dynamically linked to the MFC library, in the call to export functions, to load the corresponding resources, MFC will first find from the application module, is not found in the DLL, so if there is a resource with the same name, the error may be loaded.
Example:
Dll:mfcextdll, there is a dialog box resource idd_dialog2, it corresponds to the number is 2000, in resource.h has the following definition
#define IDD_DIALOG2 2000
The DLL has an export function that looks like this:
extern "C" __declspec (dllexport) void Showdlg (void)
{
HInstance save_hinstance = AfxGetResourceHandle ();
AfxSetResourceHandle (theinstance);
CDialog Dlldialog (IDD_DIALOG2);
Dlldialog.domodal ();
AfxSetResourceHandle (save_hinstance);
}
Application Calling DLL: Mfcextdllcall, which has a dialog box resource Idd_dialog1, and the number is 2000, in Resource.h there are the following definitions
#define IDD_DIALOG1 2000
Call the export function at the following:
void Cmfcextdllcalldlg::onbutton1 ()
{
Todo:add your control notification handler code here
typedef void (*lpfun) (void);
HINSTANCE hDLL; DLL handle
hDLL = LoadLibrary ("MFCExtDll.dll");
if (Null==hdll)
{
MessageBox ("DLL failed to load");
}
Lpfun Pshowdlg = (lpfun) GetProcAddress (hDLL, "Showdlg");
if (NULL==PSHOWDLG)
{
MessageBox ("Search for failure in DLL");
}
Pshowdlg ();
}
At this point, the pop-up dialog box is not the dialog resource DIALOG2 defined in the DLL, but rather the DIALOG1 in the program, that is, because their IDs are duplicated, causing an error loading the resource.
Solution:
If the DLL is a regular DLL, there are three methods, see "VC + + dynamic link library (DLL) programming in simple Way (All)" "three"; now is the MFC extension DLL, you can also learn from its methods, as follows:
1, when the DLL loading, that is, the DllMain function to obtain the handle of the DLL:
HINSTANCE theinstance;
extern "C" int apientry
DllMain (hinstance hinstance, DWORD dwreason, LPVOID lpreserved)
{
Unreferenced_parameter (lpreserved);
if (Dwreason = = Dll_process_attach)
{
TRACE0 ("Mfcextdll.") DLL initializing!/n ");
Extension DLL One-time initialization
if (! AfxInitExtensionModule (Mfcextdlldll, hinstance))
return 0;
New CDynLinkLibrary (Mfcextdlldll);
Theinstance = hinstance;
}
else if (Dwreason = = Dll_process_detach)
{
TRACE0 ("Mfcextdll.") DLL terminating!/n ");
Terminate the library before destructors are called
AfxTermExtensionModule (Mfcextdlldll);
}
return 1; Ok
}
2, in the export function of the resources to switch:
extern hinstance theinstance;
Exportfun
{
HInstance save_hinstance = AfxGetResourceHandle ();
AfxSetResourceHandle (theinstance);
···
AfxSetResourceHandle (save_hinstance);
}