Solved

Dialogs from a resource DLL don't work

Posted on 2004-08-05
13
1,482 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
Comment Utility
Does your OnInitDialog function get called?
0
 
LVL 5

Expert Comment

by:millsoft
Comment Utility
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
Comment Utility
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
 
LVL 6

Author Comment

by:joghurt
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

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

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
paragon account problem 9 96
countEvens challenge 2 55
only14 challenge 19 63
Unix Command -- Challenging  question 7 77
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

14 Experts available now in Live!

Get 1:1 Help Now