[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1646
  • Last Modified:

Dialogs from a resource DLL don't work

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
joghurt
Asked:
joghurt
  • 5
  • 3
  • 2
  • +1
1 Solution
 
millsoftCommented:
Does your OnInitDialog function get called?
0
 
millsoftCommented:
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
 
joghurtAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
joghurtAuthor Commented:
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
 
hirnsiebCommented:
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
 
hirnsiebCommented:
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
 
joghurtAuthor Commented:
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
 
joghurtAuthor Commented:
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
 
hirnsiebCommented:
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
 
joghurtAuthor Commented:
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
 
CetusMODCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now