Deserialization stops working if I load MFC DLL from regular DLL.
I have been asking this question before, but at that time I think it was to complex so I did not get any answer solving the problem for me. See C# .NET User Control inside native app. Resource chain problems
Now I have made a test application showing the same problem. You can find the zipped code files at https://skydrive.live.com/?#cid=0977B1167FE01BB3&id=977B1167FE01BB3%21105&sc=documents.
If I load a MFC extension DLL from a regular DLL then deserialization stop working if I call the MFC Extension DLL directly from the application.
When I try to deserialize I get the following CArchiveException "an unnamed file contained an unexpected object."
In addition the output in Visual Studio 2008 gives the following information when I run the debug version of SerializeTest:
“Warning: Cannot load CSerializableClass from archive. Class not defined. CArchive exception: badClass. First-chance exception at 0x7c812afb in SerializeTestD.exe: Microsoft C++ exception: CArchiveException at memory location 0x0012edf8.”
The following test code is run in CMFCDLL::DoIt
void CMFCDLL::DoIt(void)
{
BYTE * pBuf = (BYTE *)new char [1024];
{
CSerializableClass *pSerializableClass = new CSerializableClass;
CMemFile mf;
mf.Attach(pBuf, 1024);
CArchive ar(&mf, CArchive::store);
ar << pSerializableClass;
ar.Close();
mf.Detach();
delete pSerializableClass;
}
{
CSerializableClass *pSerializableClass = NULL;
CMemFile mf;
mf.Attach(pBuf, 1024);
CArchive ar(&mf, CArchive::load);
try
{
ar >> pSerializableClass;
ar.Close();
mf.Detach();
ASSERT(pSerializableClass && pSerializableClass->GetText() == _T("This is a serialize test"));
delete pSerializableClass;
}
catch(CArchiveException *p)
{
char str[500];
p->GetErrorMessage(str,500);
AfxMessageBox(str);
p->Delete();
ar.Abort();
mf.Detach();
}
}
delete pBuf;
}
The code that fails is "ar >> pSerializableClass". Any ideas why I get this problem?
Why do I have to use a regular DLL?
I am writing a .NET wrapper and the mixed mode DLL is a regular DLL accessing serialization in a MFC Extension DLL.