Re: Show Dialog from MFC Extension DLL
On Feb 13, 1:54 pm, Nick Meyer <NickMe...@discussions.microsoft.com>
wrote:
"David Ching" wrote:
I never understood when you export a class in an MFC Extension DLL, do =
you
have to call AFX_MANAGE_STATE at the top of every method? This is a =
real
pain. And in this case where you export a class derived from CDialog=
, the
entry point is DoModal(), which is in CDialog, and is not overridden in=
the
derived class, so how do you call AFX_MANAGE_STATE in this case?
Hi Stephen and David,
My understanding is that AFX_MANAGE_STATE is only necessary in _regular_ =
MFC
DLLs, not _extension_ DLLs. At any rate, I tried that, and I found it =
made
the linker complain about redefinition of _DllMain. Not sure why.
That is expected. You shouldnt use this macro in your case.
I did find the problem, though -- the application was actually calling a
function inside the DLL itself that was attempting to create and display =
the
dialog. I added the following:
HINSTANCE hInstOld = ::AfxGetResourceHandle();
::AfxSetResourceHandle (McfCoreDLL.hModule);
// Dialog stuff
::AfxSetResourceHandle (hInstOld);
to change the resource handle to that of the DLL, and it worked.
That is what I mentioned in my other post. You can swap resouruce
handles at will or use CDynLinkLibrary(which I prefer).
I'm not sure I understand why this was necessary though -- if I understan=
d
the MSDN docs correctly, executables can use resources from extension DLL=
s
they've loaded without knowing in which module the resources are defined.=
Tracing through the CDialog::DoModal code, I find
if (m_lpszTemplateName != NULL)
{
hInst = AfxFindResourceHandle(m_lpszTem=
plateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst,=
m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, h=
Resource);
}
executed. I thought that AfxFindResourceHandle would return the handle=
of
the DLL, not the handle of the EXE, but it seems it's returning the handl=
e of
the EXE.
If you want your resoruces to be found by MFC, you should use
CDynLinkLibrary. If you use that, you dont have to do anything special
to swap resource handles etc. It will just work as MFC will find all
the ID in the resource chain that you constructed. Resources can span
multiple modules as long as they are put together in a chain.
--
Ajay