Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Customized CPrintDialog in a DLL

Posted on 1998-05-12
5
Medium Priority
?
430 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

670 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