Solved

Customized CPrintDialog in a DLL

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 100
Detect CR LF to each line 12 137
conditional code and condition difference 9 65
Path of Workbook 3 45
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now