Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Customized CPrintDialog in a DLL

Posted on 1998-05-12
5
Medium Priority
?
431 Views
Last Modified: 2013-11-20
Hi,

I am trying to bring up a customized print dialog from within an MFC extension DLL.
What I've done is:

- copied the dialog resource from prnsetup.dlg to my .rc file
- copied the resource ids from dlgs.h to my resource.h file
- derived a class from CPrintDialog (called CSPGraphPrintDlg), and overridden the constructor like this:

CSPGraphPrintDlg::CSPGraphPrintDlg(BOOL bPrintSetupOnly, DWORD dwFlags, CWnd* pParentWnd) :
      CPrintDialog(bPrintSetupOnly, dwFlags, pParentWnd)
{
      m_pd.Flags|=PD_ENABLEPRINTTEMPLATE;
      m_pd.hInstance=AfxGetInstanceHandle();
      m_pd.lpPrintTemplateName=MAKEINTRESOURCE(IDD_GRAPHPRINTDLG);
}


I've tested this solution in a 'normal' MFC application, and it worked. However, when I
try from wihin an MFC extension DLL the dialog box does not appear and
CommDlgExtendedError() returns CDERR_FINDRESFAILURE.

I also tried replacing the line
      m_pd.hInstance=AfxGetInstanceHandle();
by
      m_pd.hInstance=AfxGetResourceHandle();
and
      m_pd.hInstance=AfxGetApp()->m_hInstance;

both with no effect.


Is  there something I have forgotten to consider ?

Thank you,

Patrick
0
Comment
Question by:ppeck
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1309990
According to the documentation:

If you have an exported function, such as one that launches a dialog box in your DLL, you need to add the following code to the beginning of the function:

AFX_MANAGE_STATE(AfxGetStaticModuleState());

This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope.

Problems with resources in DLLs will occur if the AFX_MANAGE_STATE macro is not used. By default, MFC uses the resource handle of the main application to load the resource template. This template is actually stored in the DLL. The root cause is that MFC's module state information has not been switched by the AFX_MANAGE_STATE macro. The resource handle is recovered from MFC's module state. Not switching the module state causes the wrong resource handle to be used.

For more information on module states and MFC, see "Managing the State Data of MFC Modules" documentation.
0
 
LVL 1

Author Comment

by:ppeck
ID: 1309991
Dear chensu,

since I am using an MFC extension DLL I think I don't need the AFX_MANAGE_STATE
macro because
1) I have succeeded to bring up lots of dialogs from within my DLL without
the AFX_MANAGE_STATE macro
2) using the AFX_MANAGE_STATE macro gives me a link error:
   mfcs42d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in testdll.obj

My problem only concerns the print dialog.

Patrick

0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 300 total points
ID: 1309992
All of these will proberly give you the save HINSTANCE walue (of the EXE):

1:  m_pd.hInstance=AfxGetInstanceHandle();
2:  m_pd.hInstance=AfxGetResourceHandle();
3:  m_pd.hInstance=AfxGetApp()->m_hInstance;

Instead try setting m_pd.hInstance to the instance of your DLL (The HINSTANCE you get in DllMain())



0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1309993
You can also set the change App-wide resource handle temporarely:

HINSTANCE hOld = AfxGetResourceHandle();
AfxSetResourceHandle( hDllInstance );

//
// Init and show dialog here
//


AfxSetResourceHandle( hOld );

0
 
LVL 1

Author Comment

by:ppeck
ID: 1309994
Wow!

Finally, that worked. I discovered that there is a variable of
type AFX_EXTENSION_MODULE near the DllMain function,
where the instance handle of the DLL is stored.

I would have preferred a cleaner solution than reading some
global variable, but it seems there is no other possible way to do it...
(the AfxGetStaticModuleState() function won't link)

Nevertheless, thanks a lot !

Patrick

P.S.: the next chapter of this story will be dealing with the custom
controls I want to add to the dialog...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question