Solved

Customized CPrintDialog in a DLL

Posted on 1998-05-12
5
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
pre4 challenge 19 116
My project did see openJDK that I installed. What could be the problem 7 172
Path of Workbook 3 88
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 135
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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