0
votes

I've migrated *MFC* project from VC 6 to VS 2010 project. Now a custom dialog we implemented uses the CDialog::DoModal and it isn't working and it fails at this line HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG) As in the new project it returns Null and in the old it gets a value. - The project builds normally so I don't think I forget to import a resource? - I am using ribbons in the new project if this could have relation with the problem? - Should I change the inheritance of the custom dialog to CDialogEx?

Updated I created a new custom dialog that inherits CDialog and DoModal worked very well The problem are with those lines exactly in the DoModal

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

As hInst doesn't return normally so all other callings fail. The issue is related to using dialogs and resources that are in another dll. I still don't understand why did the problem appear in VS2010 and didn't appear in VS 6. And How could This issue be resolved!

5
LPCTSTR m_lpszTemplateName; For CDialog, CDialogBar and CFormView in afxwin.h and afxext.hBassam Gamal

5 Answers

1
votes

Well, it's not finding the resource, so there are two possibilities:

1) The resouce isn't in your executables 2) The module containing the resource is not in the MFC resource chain

First, open the .exe or .dll file you think the resource is in in the IDE. When you open the module in the IDE it will open as a resource file. Search through the dialogs in the module and verify that it really is in the module.

If you did verify that the resource is in the module, then before the call to DoModal() make a call to AfxSetResourceHandle() with the HINSTANCE of the module containing the resource.

0
votes

I think you are getting your HINSTANCE incorrectly. You can get the HINSTANCE as follows:

HINSTANCE hInstance = (HINSTANCE)GetModuleHandle( NULL );

As such the following code ought to work:

hInst = (HINSTANCE)GetModuleHandle( NULL );
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
0
votes

Try changing the assigned number for your IDD_YOURDIALOG. Maybe it conflicts with some new resource in MFC 10.0?

0
votes

May be too late, but I found this post because I had a similar problem. The problem is in the Vista dialog style, which is default in VS2010. I just added two zeros in base class initialization, and it start working as before:

IMPLEMENT_DYNAMIC(LoadAscii_dlg, CFileDialog)

LoadAscii_dlg::LoadAscii_dlg(LPCTSTR lpszFileName) :
        CFileDialog( TRUE, "txt", lpszFileName, 
        OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_ENABLETEMPLATE | OFN_EXPLORER,
        "Text Files (*.txt)|*.txt|Comma Separated Values (*.csv)|*.csv|All Files (*.*)|*.*||", 0,**0,0** )

{
    m_ofn.lpTemplateName = MAKEINTRESOURCE( IDD_LOADASCII );
....
}
0
votes

There was a problem with resources which were missing. 1- Added the appropriate rc files to project that shown problem. 2- Solved some duplication in the resources.

Still didn't get why it was working in the old project and failed in the new one.