MFC Dialog box not working - VC6 upgrade
Large project that I've been working on converting from VC++ 6.0 to VC++ 8.0 (.NET 2005). It has an app (EXE), a main DLL, and 5 component DLLs (formerly COM, but now are MFC Dlls).
One of the DLLs has many dialogs and controls that are used. When I converted over, I just copied most of the resource file (.rc) over to the new one (textually).
Everything looks fine. All dialog boxes and controls appear fine when I look at the resources in the project.
When I get to the point in my code where a dialog box is called, nothing happens. Nothing comes up, it just goes over the line returning a -1.
Relevant Code:
Code:
CSequenceSelectDialog dlg;
dlg.m_pTestProperties = m_pTestProperties;
dlg.m_pConfigurationMatrix = m_pConfigurationMatrix;
dlg.m_nStationNumber = nStationNumber;
dlg.m_strUserPath = m_strUserPath;
dlg.m_strTestPath = m_strTestPath;
before .NET conversion */
*nSequenceSelection = m_nSequenceSelection = (int) dlg.DoModal();
if (*nSequenceSelection == IDOK)
{
Is there any reason this isn't working now that you can think of? Any thoughts are appreciated.
Re: MFC Dialog box not working - VC6 upgrade
Went further and debugged in to Windows code.
This is the DoModal() code.
Code:
INT_PTR 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);
}
It looks like the ::FindResource doesn't fine anything. hResource is NULL returning from that routine.
GetLastError returns: 1812 - ERROR_RESOURCE_DATA_NOT_FOUND (The specified image file did not contain a resource section.)
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
Is there any reason this isn't working now that you can think of?
The code you posted will not help, as all it does is show us that your syntax is OK. It doesn't tell us what is happening at runtime or anything else that would give any indication what the problem is.
If this were a VC 6.0 application, what reasons are there for returning a -1 for DoModal()? Whatever those reasons are, did you investigate whether those are the same reasons with your current application?
One reason is that a control in your dialog is not available or not set up properly. The easiest way to detect this is to remove all controls from the dialog, temporarily comment out any code that might refer to the controls, and see if the empty dialog displays correctly when you run the application. If it does, then add controls one at a time until it breaks.
On a side note, this is suspicious:
Code:
*nSequenceSelection
Did you initialize this pointer? If you didn't, that code where you are setting the return value of DoModal is faulty.
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
Went further and debugged in to Windows code.
What is the value of hInst? Is it the handle to the module that contains the dialog resource? If not, that is the reason for the error.
What is m_lpszTemplateName? Is it the name of the dialog resource? If not, again, an error.
When you compiled, did the resource compiler actually compile any resources?
When you open the DLL in a resource editor, do you see the dialog as one of the resources (I believe you can do this with Visual Studio by specifying that you are opening the DLL as a resource).
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
Paul McKenzie
What is the value of hInst? Is it the handle to the module that contains the dialog resource? If not, that is the reason for the error.
What is m_lpszTemplateName? Is it the name of the dialog resource? If not, again, an error.
When you compiled, did the resource compiler actually compile any resources?
When you open the DLL in a resource editor, do you see the dialog as one of the resources (I believe you can do this with Visual Studio by specifying that you are opening the DLL as a resource).
Regards,
Paul McKenzie
hInst => 0x05C90000 (unused)
I'm not sure the DLL handle, but the 'unused' makes me think it isn't the DLLs handle.
m_lpszTemplateName = 0x000000d0 <Bad Ptr>
Again, "Bad Ptr" doesn't look good.
Ok, looking closer at the copmiler statements and I think I see what you mean;
Code:
1>Generating Code...
1>Compiling resources...
1>Linking...
That looks like no resources are getting compiled.
OK, how do I fix that? What to look at to get those compiled?
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
Paul McKenzie
When you open the DLL in a resource editor, do you see the dialog as one of the resources (I believe you can do this with Visual Studio by specifying that you are opening the DLL as a resource).
Regards,
Paul McKenzie
Resource editor? Is that a VS tool?
Re: MFC Dialog box not working - VC6 upgrade
Ok, I downloaded a resource editor once I figured out what you meant (and that it was a separate program).
The resource editor shows the dialog components in the DLL just fine.
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
Resource editor? Is that a VS tool?
If you do a File / Open in Visual Studio, and you open the DLL file you claim has the resource, the DLL file will be loaded, and you will see the resources.
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
Ok, I downloaded a resource editor once I figured out what you meant (and that it was a separate program).
The resource editor shows the dialog components in the DLL just fine.
What is the resource ID of the dialog? Is it numeric, or is it a string name?
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
Paul McKenzie
What is the resource ID of the dialog? Is it numeric, or is it a string name?
Regards,
Paul McKenzie
All the resource IDs looked numeric. Each dialog had a number associated with it.
http://img22.imageshack.us/img22/7418/rdnumericp.jpg
Re: MFC Dialog box not working - VC6 upgrade
Have to run home (late day already)....hopefully pick back up on this tomorrow. Thanks for the help so far. :)
Re: MFC Dialog box not working - VC6 upgrade
Check out AFX_MANAGE_STATE here
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
All the resource IDs looked numeric. Each dialog had a number associated with it.
Where does the code come from in your first post? The resource certainly cannot be found in that module where the code you posted comes from.
A DLL that contains resources must be loaded at runtime via LoadLibrary by the application. Is this how you're accessing the resources in the DLL?
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
VladimirF
Check out AFX_MANAGE_STATE
here
Yep, we're thinking on the same level. The MFC way to do the LoadLibrary is the AFX_MANAGE_STATE.
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
All the resource IDs looked numeric. Each dialog had a number associated with it.
And 0xd0 in the FindResource is 208, so the dialog being searched for is correct, in terms of ID.
Therefore the most probable reason is what Vladimir and myself pointed out -- the module where that code is being called from does not have the resources embedded within it -- it needs to go to an "external" library for the resource.
To do that using MFC, the AFX_MANAGE_STATE sets the resource handle (the hInst in the FindResource) to the instance handle of the DLL that contains the resources.
The Windows API way to do this is to call LoadLibrary() and store the HINSTANCE that is returned -- this then becomes the "hInst" in the FindResource function.
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Thanks....looking in to the AFX_MANAGE_STATE macro now.
That's something I've seen littered here and there in these DLLs before, but never really knew what it did.
Re: MFC Dialog box not working - VC6 upgrade
I'm still a little confused on exactly how the AFX_MANAGE_STATE works.
The previous architecture was an APP that called a (MFC) DLL that called this DLL (COM component) to display the resources within a function call.
Now its similar, but without COM. APP->(MFC)DLL->MFC DLL.
I tried the AFX_MANAGE_STATE(AfxGetStaticModuleState( )); macro, but to no avail. From another page I looked up and from your comments and what I read on the MS page linked by Vladimir, I'm thinking I need to run in the context of the main app when I open the dialog. Is that right?
I'm seeing an example on an MSDN forum post that shows passing in the Window Handle (from the APP in my case I'm guessing) and using that in the constructor of the Dialog. Does it sound like I'm on the right track at all?
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
I'm still a little confused on exactly how the AFX_MANAGE_STATE works.
My personal experience is that I don't use MFC to handle resources that are found in other modules. I create DLL's (non-MFC DLL's) that need resources loaded from other DLL's, and I have never used anything but the low-level API's to handle the resources.
It's one of the few cases where IMO it's easier doing this the low-level API way -- just storing HINSTANCE handles of your modules, and then calling LoadResource(), FindResource(), LoadString() etc. with the appropriate instance handle. You have control over which module's resources gets loaded, displayed, etc. instead of relying on what amounts to a "global" setting.
In my situation, it isn't easy (next to impossible really) to control resources using the AFX_MANAGED_STATE macro (but in most situations other than what I use resources for, it works).
Regards,
Paul McKenzie
Re: MFC Dialog box not working - VC6 upgrade
Quote:
Originally Posted by
KingTermite
Large project that I've been working on converting from VC++ 6.0 to VC++ 8.0 (.NET 2005). It has an app (EXE), a main DLL, and 5 component DLLs (formerly COM, but now are MFC Dlls).
One of the DLLs has many dialogs and controls that are used. When I converted over, I just copied most of the resource file (.rc) over to the new one (textually).
Everything looks fine. All dialog boxes and controls appear fine when I look at the resources in the project.
When I get to the point in my code where a dialog box is called, nothing happens. Nothing comes up, it just goes over the line returning a -1.
Relevant Code:
Code:
CSequenceSelectDialog dlg;
dlg.m_pTestProperties = m_pTestProperties;
dlg.m_pConfigurationMatrix = m_pConfigurationMatrix;
dlg.m_nStationNumber = nStationNumber;
dlg.m_strUserPath = m_strUserPath;
dlg.m_strTestPath = m_strTestPath;
before .NET conversion */
*nSequenceSelection = m_nSequenceSelection = (int) dlg.DoModal();
if (*nSequenceSelection == IDOK)
{
Is there any reason this isn't working now that you can think of? Any thoughts are appreciated.
Why did you copy resoure file textually?
I would start an empty project and copy over the files. I haven't used VC2005 but shouldn't it convert V6S project files?
Editing resource files is nothing but trouble.
Start and empty project and copy the files over. I have several large V6S, if I have to copy over the resource file manually for about 60 dialog boxes I'm in trouble.
Try creating some small projects in V6S and covert them to get idea how to convert them. ... I still think that VS2005 should automattically convert V6 projects, maybe only V2003 does it.
...either way, just copying the project files should work though, stay in the C++ world.