Solved

Customized CPrintDialog in a DLL

Posted on 1998-05-12
5
415 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replacement selected text 2 65
Shell script to login to remote m/c and execute commands 10 78
ffmpeg - "rtsp://...... Operation not permitted" 4 88
firstChar challenge 13 131
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 (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

679 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