3
\$\begingroup\$

I have a CArchive Which contains a serialized CMapStringToString.

From my research it is my understanding that it is quite difficult to directly read a CArchive from within a C# DLL. So I decided to read it in the MFC application and feed it into the DLL interface.

At the moment I am doing the following:

m_pInterface->UpgradeDatabase();
CFile fileNotes;
CFileException ex;
TCHAR szError[_MAX_PATH];
CMapStringToString mapSSBrotherNotes;
if (fileNotes.Open(_T("d:\\BrotherNotes.BIN"), CFile::modeRead, &ex))
{
 TRY
 {
 CArchive ar(&fileNotes, CArchive::load);
 mapSSBrotherNotes.Serialize(ar);
 ar.Close();
 POSITION sPos = mapSSBrotherNotes.GetStartPosition();
 while (sPos != NULL)
 {
 CString strName, strNotes;
 CComBSTR bstrName, bstrNotes;
 mapSSBrotherNotes.GetNextAssoc(sPos, strName, strNotes);
 bstrName = strName.AllocSysString();
 bstrNotes = strNotes.AllocSysString();
 m_pInterface->SetPublisherNotes(bstrName, bstrNotes);
 }
 }
 CATCH(CException, pEx)
 {
 pEx->GetErrorMessage(szError, _MAX_PATH);
 AfxMessageBox(szError, MB_OK | MB_ICONERROR);
 }
 END_CATCH
 fileNotes.Close();
}
else
{
 // If the file was not found, this is probably not an error
 if (ex.m_cause != CFileException::fileNotFound)
 {
 ex.GetErrorMessage(szError, _MAX_PATH);
 AfxMessageBox(szError, MB_OK | MB_ICONERROR);
 }
}
m_pInterface->SavePublisherData(&iResult);

It works fine but is there a better way to read this data file from within the C# DLL?

Side question - can my use of CString / CComBSTR be simplified in my current code?

asked Jan 4, 2017 at 10:38
\$\endgroup\$
4
  • \$\begingroup\$ @t3chb0t The way I read this question, it seems OK. Could just do with re-wording a little. \$\endgroup\$ Commented Jan 4, 2017 at 10:45
  • 2
    \$\begingroup\$ Write a C++/CLI wrapper for it. It'll be c++ inside and .net outside so you can easily use it from c# \$\endgroup\$ Commented Jan 4, 2017 at 10:49
  • \$\begingroup\$ @t3chb0t Can you please advise me on this concept? Thanks. \$\endgroup\$ Commented Jan 4, 2017 at 11:06
  • 3
    \$\begingroup\$ well, I'd say read C++/CLI in Action and get started ;-) \$\endgroup\$ Commented Jan 4, 2017 at 11:11

1 Answer 1

1
\$\begingroup\$

But is there a better way to read this data file from within the C# DLL?

You can write a C++/CLI wrapper. It'll compile as a .NET assembly that you can reference in your project as any other .NET dll but inside it'll be able to work with `C++ and use your current code as it is.

Visual C++ supports interoperability features that allow managed and unmanaged constructs to co-exist and interoperate within the same assembly, and even in the same file.

Mixed assemblies are capable of containing both unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged. Using mixed assemblies, developers can author applications using a mixture of managed and unmanaged functionality. This makes mixed assemblies ideal for migrating existing Visual C++ applications to the .NET Platform.

Mixed (Native and Managed) Assemblies

I recommend reading

answered Jan 10, 2017 at 18:34
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.