Click to See Complete Forum and Search --> : Dialogs in dll's


April 2nd, 1999, 11:07 AM
I'm trying to create my own version of an MFC Common Dialog. I can get the code into a workable dll, but I can't get the dialog box itself to appear unless I create the dialog box in the rc file of the calling program. I want to avoid that and have the dll create the dialog box itself, much like if you create the CPrintDlg you don't have to redefing the dialog box. I CANNOT MAKE THIS WORK. Any ideas? I'm getting frustrated...

April 2nd, 1999, 12:06 PM
Typically you need a handle to the instance of the module that contains the dialog resource.
In the past I have resorted to implementing a DllMain function (check docs on this) and
I saved the hInstance parameter when it is called for the DLL_PROCESS_ATTACH reason.
I then make a function called MyLibInstance(); that is called to obtain that instance. I often
write a little wrapper function for calling the dialog to encapsulate all of this and also so that
I don't need to include the dialog class header in the calling app - just a prototype of the
wrapper

Yet another method that can be used in addition or in place of the above is to make a DLL
intializer function. It purpose is to place the instance of the library in the MFC resource
list. AfxLoadModule() will traverse the list and look for the specified resource in the
list. This forces you to have unique resource IDs for all dialogs. This option also rules
out using the dialog in a non-MFC app (in case you care :). Here is a snippet of a
library initializer I have used. I use the ifdefs to make sure I link with the correct build
type of the library. Of course, you can force the library to have different names for
different builds and link with them as appropriate but this is old code. I hope this is
helpful and not too baffling. Just ask if you need more help.

Gomez Addams - the new board doesn't like me very much ;-|

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\
// in a header

#define DllImport __declspec(dllimport)

// call InitMfcLib() in the InitInstance of the app.

#ifdef _DEBUG
DllImport void WINAPI dbgInitMfcLib();
#define InitMfcLib() dbgInitMfcLib()
#else // _DEBUG
DllImport void WINAPI relInitMfcLib();
#define InitMfcLib() relInitMfcLib()
#endif // _DEBUG

// library source code

static AFX_EXTENSION_MODULE NEAR extensionDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if( dwReason == DLL_PROCESS_ATTACH )
{
// Extension DLL one-time initialization - do not obtain memory here,
// use the TRACE or ASSERT macros or call MessageBox
if( ! AfxInitExtensionModule(extensionDLL, hInstance) )
return 0;
}
return 1; // ok
}

// Exported DLL initialization is run in context of running application

#define DllExport __declspec(dllexport)

#ifdef _DEBUG
DllExport void WINAPI dbgInitMfcLib()
#else // _DEBUG
DllExport void WINAPI relInitMfcLib()
#endif // _DEBUG
{
// create a new CDynLinkLibrary for this app
new CDynLinkLibrary(extensionDLL);
// nothing more to do
}