Showing Dialogs from a DLL
Marcus -- bmarcus@erols.com
Tuesday, May 07, 1996
System: NT3.51, service pack 3; VC++ 4.1
I have a question concerning dialogs and dlls. I'm in the process of converting what was once a
large .EXE (containing many dialogs in its resource file) that will now be a small .EXE that calls a
large .DLL (that will contain most of the previous dialogs). The DLL is to have the dialog
resources and the EXE will call a function (DLL entry point) that will show a particular DLL dialog.
The calling function from the EXE looks like this:
void CMass32View::OnShowTestDlg()
{
ShowMassDBDlg(this->m_hWnd,INDEX);
}
The function in the DLL looks like this:
extern "C" __declspec( dllexport ) BOOL ShowMassDBDlg(HWND hWndParent,int dialog)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
TRY
{
switch ( dialog )
{
case INDEX:
CSensorDB SensorDialog;
int result = SensorDialog.DoModal();
if (result == -1)
AfxMessageBox("Couldn't create dialog");
else if (result == IDABORT)
AfxMessageBox("Some other error with dialog");
else
AfxMessageBox("This should have worked");
:
:
:
The problem is that DoModal() is always returning -1 (can't create the dialog). Why???
Please post response or e-mail me directly at bmarcus@erols.com
Thanks,
Brad Marcus
JCS -- JCS@ficsgrp.com
Friday, May 10, 1996
[Mini-digest: 3 responses]
This is a problem concerning resources that are not available, it think.
You should include the following rule in your main header file (the one
defining the class derived from CWinApp)
extern HINSTANCE the_hinstDLL;
in the corresponding .cpp file you then define
HINSTANCE the_hinstDLL;
and override the :InitInstance() function as followes:
BOOL CAskApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
the_hinstDLL = AfxGetInstanceHandle();
return CWinApp::InitInstance();
}
then, before calling your Modal() function, set the resource handle:
HINSTANCE previous_hinst = AfxGetResourceHandle();
AfxSetResourceHandle(the_hinstDLL);
CSensorDB SensorDialog;
int result = SensorDialog.DoModal();
...
AfxSetResourceHandle(previous_hinst);
Hope this helps
Jan Castermans
-----From: joew@statsoftinc.com (Joe Willcoxson)
Try using AfxSetResourceHandle() and AfxGetResourceHandle(). Your DoModal
is probably failing because you're app can't find the dialog resource in
your .EXE file. If you set the resource handle to your DLL instance, then
it will be able to find it. However, don't forget to set it back.
Joe Willcoxson (joew@statsoftinc.com)
http://www.statsoftinc.com
Author of the FREE WorldCom MFC library:
http://users.aol.com/chinajoe/wcmfclib.html
-----From: Jeff Lindholm
You need to ensure that you are using the DLL's resources.
Call AfxSetResource() to the DLL's instance and then set it back to the
Application's after you create the dialog.
Jeff
David Mcalister -- davidm@rackwick.demon.co.uk
Friday, May 10, 1996
----------
From: Marcus
Sent: 07 May 1996 20:40
To: MFC list
Subject: Showing Dialogs from a DLL
System: NT3.51, service pack 3; VC++ 4.1
I have a question concerning dialogs and dlls. I'm in the process of converting what was once a
large .EXE (containing many dialogs in its resource file) that will now be a small .EXE that calls a
large .DLL (that will contain most of the previous dialogs). The DLL is to have the dialog
resources and the EXE will call a function (DLL entry point) that will show a particular DLL dialog.
The calling function from the EXE looks like this:
void CMass32View::OnShowTestDlg()
{
ShowMassDBDlg(this->m_hWnd,INDEX);
}
The function in the DLL looks like this:
extern "C" __declspec( dllexport ) BOOL ShowMassDBDlg(HWND hWndParent,int dialog)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
TRY
{
switch ( dialog )
{
case INDEX:
CSensorDB SensorDialog;
int result = SensorDialog.DoModal();
if (result == -1)
AfxMessageBox("Couldn't create dialog");
else if (result == IDABORT)
AfxMessageBox("Some other error with dialog");
else
AfxMessageBox("This should have worked");
:
:
:
The problem is that DoModal() is always returning -1 (can't create the dialog). Why???
Please post response or e-mail me directly at bmarcus@erols.com
Thanks,
Brad Marcus
I enclose an excerpt from comp.os.ms-windows.programmer.tools.mfc
pat@eco.twg.com wrote:
>
> I've written a simple MFC based DLL which is in turn used by
> another MFC application.
>
> When the MFC based DLL is statically linked with the MFC library
> my application performs as expected. (i.e. I can bring up a dialog box
> from within the DLL)
>
> When I change the General project settings such that the DLL uses
> MFC in a shared DLL I can no longer bring up the same dialog box.
> The following messgage appears in the output window:
> "First-chance exception in test_mfc_dll.exe (NTDLL.DLL): 0xC0000005: Access Violation."
>
> What concepts or "MFC features" am I missing.
>
The problem is with loading the dialog's resources. This question seems
to come up fairly frequently, and I've posted the solution a couple of
times in the last couple of months. I assume there's a FAQ for this
group, but since I have no details on where to get it, nor whether
this info is there, I'll post it again.
------------------------------------------------------------
What you need to do is get the instance handle of your DLL and make
that the current app resource handle. To do that, add the following
code to your DLL:
// Instance handle of this DLL
static HINSTANCE hInst_DLL;
/////////////////////////////////////////////////////////////////////////////
// Initialization of MFC Extension DLL
static AFX_EXTENSION_MODULE NEAR extensionDLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
hInst_DLL = hInstance; // save for later
if (dwReason == DLL_PROCESS_ATTACH)
{
if (!AfxInitExtensionModule(extensionDLL, hInstance))
return 0;
}
return 1; // ok
}
This will get called automatically when the dll is loaded.
In your function that calls DoModal() do the following:
{
// set resource handle to this dll, so our resources can be found
HINSTANCE hOldRes = AfxGetResourceHandle(); // save for restoring
AfxSetResourceHandle(hInst_DLL);
CMyDialog dlg;
dlg.DoModal();
AfxSetResourceHandle(hOldRes); // restore app's handle
}
BTW, I got most of this from the DLLHUSK sample that comes with VC4.0.
Ha
begin 600 WINMAIL.DAT
M>)\^(C$(`0:0" `$```````!``$``0>0!@`(````Y 0```````#H``$(@ <`
M& ```$E032Y-:6-R;W-O9G0@36%I;"Y.;W1E`#$(`0V ! `"`````@`"``$$
MD 8`# $```$````,`````P``, (````+``\.``````(!_P\!````/P``````
M``"!*Q^DOJ,0&9UN`-T!#U0"`````&UF8RUL0&YE=&-O;2YC;VT`4TU44 !M
M9F,M;$!N971C;VTN8V]M```>``(P`0````4```!33510`````!X``S !````
M$0```&UF8RUL0&YE=&-O;2YC;VT``````P`5# $````#`/X/!@```!X``3 !
M````$P```"=M9F,M;$!N971C;VTN8V]M)P```@$+, $````6````4TU44#I-
M1D,M3$!.151#3TTN0T]-`````P``.0`````+`$ Z`0````(!]@\!````! ``
M``````(]+ $$@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