Solved

Using MFC Dialog in a Win32 DLL

Posted on 1997-07-02
6
993 Views
Last Modified: 2013-11-19
I want to use the U.I. of MFC to create a Dialog Box as
part of a generic win32 DLL.
 
Example: I have a win32 DLL that draws Graphs. I would like
 to be able to configure the graph by making a call to a
 member function of the DLL that pops up a configuration
 dialog.

My crude attempt appears that the DLL resources are not
accessible (DoModal() returns -1). Any suggestions?
0
Comment
Question by:rocco
[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
6 Comments
 
LVL 4

Expert Comment

by:AVaulin
ID: 1302441
Be sure that you set resource handler to DLL before using dialog resource from this DLL. AfxSetResourceHadle function do this.
0
 

Author Comment

by:rocco
ID: 1302442
I have tried that. It did not make a difference. The DLL
was initial created as a win32 DLL, then adding the shared
MFC library. There seems to be a conflict when _USRDLL is
defined.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1302443
Call AFX_MANAGE_STATE(AfxGetStaticModuleState( )) before loading
resources:

// ...
AFX_MANAGE_STATE(AfxGetStaticModuleState( ))
MyDlg dlg;
int ret = dlg.DoModal();
// ...
0
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!

 

Author Comment

by:rocco
ID: 1302444
That is true if the class is derived from CWinApp
(MFC dll). In doing so, you will get multiple declared
symbols (_DllMain@12).

I am starting with a generic C++ class (via Win32 dll)
and adding a MFC dialog (CDialog) to provide an GUI
interface. The attempt is to make UI classes separate
from the application classes (in this case the main DLL
class that calls a dialog through a member function.

0
 

Accepted Solution

by:
rpb earned 100 total points
ID: 1302445
If you derive a class from MFC (which you are doing here by having a class derive from CDialog) then you are extending the class hierarchy.  To get this to work you need to make sure you are building an MFC extension DLL.  I don't know if you can do it otherwise, because it will fail to load the resource.  MFC expects there to be an MFC "App" somewhere around to access information from, so if you build a DLL to be used generically, you need to make it an extension dll.

One other way to do this would be to override DoModal(), etc., and alter the way it loads the resource by explicitly loading it from the dll's resources (rather than the apps).  MFC walks down a line of CDynLinkLibrary objects to find resources, unless you use the AFX_MANAGE_STATE macro mentioned earlier.  You could override the dialog-template loading and load it yourself, but this may mean copying a chunk of code out of MFC and making a few tweaks in your version.  If you find the bit that is failing then you can just modify that bit, and assuming it is the resource-loading then that can be patched to load the correct template.

It's probably this bit at the top of DoModal():

  hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
  HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
  hDialogTemplate = LoadResource(hInst, hResource);

0
 

Author Comment

by:rocco
ID: 1302446
This sounds like were the problem lies. In order to create a
DLL that support a MFC Dialog, some kind of "App" needs to
be made available. In an extension DLL, the Application's
App class is used. For a regular MFC DLL, the DLL is derived
from CWinApp. Which gets into the reason I work from the Win32
DLL side, keeping the MFC code to a limited area... Any suggestion?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
deburging in oracle form 12 119
move a line in eclipse 3 111
Fibonacci challenge 11 141
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.

734 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