Solved

Dialogs from a resource DLL don't work

Posted on 2004-08-05
13
1,521 Views
Last Modified: 2013-11-20
I have an MFC application that displays a modal dialog. It works fine as long as the resources are in the application.

If I try to use the resource DLL, I call ::AfxSetResourceModule in the application's InitInstance method.
Strings and menus work fine - they are found from the resource DLL. However, dialogs don't appear. There's no assertion, no debug messages, etc.

I even tried the following (I've read it somewhere):

int CMyDialog::DoModal (void)
{
      m_hInstSave = afxCurrentInstanceHandle;
      afxCurrentInstanceHandle = ::AfxFindResourceHandle (MAKEINTRESOURCE (CMyDialog::IDD), RT_DIALOG);

      return CDialog::DoModal ();
}

and

BOOL CMyDialog::OnInitDialog (void)
{
      afxCurrentInstanceHandle = m_hInstSave;

      if (!CDialog::OnInitDialog ())
            return FALSE;
...
}
0
Comment
Question by:joghurt
  • 5
  • 3
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:millsoft
ID: 11731208
Does your OnInitDialog function get called?
0
 
LVL 5

Expert Comment

by:millsoft
ID: 11731295
I'd trace through CDialog::DoModal() and carefully observe this code:

/// snip...
int CDialog::DoModal()
{
    // can be constructed with a resource template or InitModalIndirect
    ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
        m_lpDialogTemplate != NULL);

    // load resource as necessary
    LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
    HGLOBAL hDialogTemplate = m_hDialogTemplate;
    HINSTANCE hInst = AfxGetResourceHandle();
    if (m_lpszTemplateName != NULL)
    {
        hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
        HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
        hDialogTemplate = LoadResource(hInst, hResource);
    }
    if (hDialogTemplate != NULL)
        lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

    // return -1 in case of failure to load the dialog template resource
    if (lpDialogTemplate == NULL)
        return -1;

...
/// snip

see if AfxGetResourceHandle() is returning what you think.  Ditto for the resource loading functions.
0
 
LVL 6

Author Comment

by:joghurt
ID: 11733295
Sorry for missing these important infos. OnInitDialog isn't called.

I've debugged into DoModal, and everything seems to be OK there. The hInst is the one I expect.
However, CWnd::CreateDlgIndirect fails for some reason. If I debug into this function, hWnd = ::CreateDialogIndirect returns NULL, but ::GetLastError returns 0.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 6

Author Comment

by:joghurt
ID: 11734002
I'm on trail.
I've found the following: http://www.pallium.com/bryan/programming.php

"I've got a modeless dialog I'm trying to create with CreateDialog, CreateDialogIndirect, or CreateDialogParam. But the function returns NULL, and GetLastError() returns 0 (how helpful). How do I figure out what the problem is with the dialog?

    * Generally this occurs because the creation of some child control of the dialog failed. But which one?
          o If you've got an RTF control, chances are, that's your man. Try deleting that control from your dialog template and re-running the application. If it works, make sure you're defining the right version constant for the 2.0 RTF control (e.g. _RICHEDIT_VER should be set to 0x0200 or more), and you did the necessary LoadLibrary call at startup.
          o Another approach to finding the baddie is to allow creation of the dialog to continue even though one or more child controls could not be created. To achieve this, add the DS_NOFAILCREATE style to your dialog template (it's called "No Fail Create" in the VS.NET IDE), and re-run your app. You'll still crash, but hopefully later when you try to do something with some dialog child control, and it isn't there. Then see if you can determine the dialog item ID from that, and you've found your baddie. Or, now that you've got the dialog's window handle, fire up Spy++ and walk the children until one you're expecting is missing: dat's da baad mon."

After using DS_NOFAILCREATE it turned out that the problem is with a grid control. After more searching I've found the reason:
http://www.codeproject.com/miscctrl/gridctrl.asp?df=100&forumid=18&fr=126&select=818030
0
 
LVL 1

Expert Comment

by:hirnsieb
ID: 11735808
Before you can use resources from an DLL you must set the resource handle using AfxSetResourceHandle. I think you must call AfxSetResourceHandle with the handle of the resource DLL before you call DoModal, because DoModal does the actual loading of the resource. Good luck!


check this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_afxsetresourcehandle.asp
0
 
LVL 1

Expert Comment

by:hirnsieb
ID: 11735833
Your code would look like this then:

int CMyDialog::DoModal (void)
{
     m_hInstSave = afxCurrentInstanceHandle;
     
HINSTANCE hResourceDLL = LoadLibrary("myResource.dll");
AfxSetResourceHandle(hResourceDLL);

     return CDialog::DoModal ();
}
0
 
LVL 6

Author Comment

by:joghurt
ID: 11736047
hirnsieb, if you read my original post carefully, as well as my follow-up comments, you'd see that not that's the problem.

Anyway, I've solved the problem myself, and posted it here:
http://www.codeproject.com/miscctrl/gridctrl.asp?forumid=18&select=891405&df=100&fr=126#xx891405xx
0
 
LVL 6

Author Comment

by:joghurt
ID: 12147114
I think this question is clear: I've solved it myself. See my comments below
 Date: 08/06/2004 11:51AM CEST
 Date: 08/06/2004 04:45PM CEST

And there is no need to blackmail me with NEGATIVE FEEDBACK, since I've found no option to accept one of my own comments, or close this question myself.
0
 
LVL 1

Expert Comment

by:hirnsieb
ID: 12155311
This is something I only can agree to! If no solution is found, the administrators ask you to close the question or split points or whatever. However you CANNOT close the question (or where's the hidden link?). This must be improved by you admin guys!
0
 
LVL 6

Author Comment

by:joghurt
ID: 12155908
From my previous experience I know I can beg some admins in the EE community forum to close/delete the question.
However (and this is a most stupid thing), if a question is deleted, all its comments that could have been helpful in a similar situation are also deleted.

But here's your secret link to show you the light in such situations: http://www.experts-exchange.com/Programming/Programming_Languages/MFC/help.jsp#hi70
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 12170441
PAQed, with points refunded (250)

joghurt,
It seems like you missunderstood the recommendation by DanRollins.
PAQ is short for "Previously Asked Question" which means questions stored in the database as they have a solution. The recommendation also included a refund of points to your account which I've hereby done.

Negative Feedback can be added to people who post questions and receive answers but never follow up or close their questions properly. You don't apply to any of these criteria so no Negative Feedback has been added to your account.

Next time when you solve your own question, please take a look here:
http://www.experts-exchange.com/help.jsp#hi70

Thanks,

CetusMOD
Community Support Moderator
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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