Solved

Using MFC Dialog in a Win32 DLL

Posted on 1997-07-02
6
935 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
6 Comments
 
LVL 4

Expert Comment

by:AVaulin
Comment Utility
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
Comment Utility
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
Comment Utility
Call AFX_MANAGE_STATE(AfxGetStaticModuleState( )) before loading
resources:

// ...
AFX_MANAGE_STATE(AfxGetStaticModuleState( ))
MyDlg dlg;
int ret = dlg.DoModal();
// ...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:rocco
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 video discusses moving either the default database or any database to a new volume.

744 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

17 Experts available now in Live!

Get 1:1 Help Now