1

We are rewriting an old MFC VC++ UI component in C# .NET

there are multiple applications involved here which share data using binary serialized files created using CArchive functionality provided by MFC.

New requirements only ask us to rewrite UI component without changing anything is current business logic. As a solution for this issue, we are planning to change MFC binary serialization with boost XML serialization so that that same XML serialized data can be processes by C# XML parser and existing functionality remains as it is.

Two questions here :

1) Will there be any issues at boost end as we are using a different XML parser at C# end ?

2) How can we minimize code changes in existing application ?

I had a look at "Boost::Serialization and MFC Doc/View architecture" link and it looks like a great way to change existing binary serialized files to XML format.

We have around 20 classes derived from one base class, all of which are serialized. Any pointers to have minimum code changes will be of great help. Thanks.

Community
  • 1
  • 1
bhardwajhp
  • 43
  • 1
  • 8
  • 2
    The Q&A you linked to is great, as long as your classes do not contain classes, that you do not control (e.g. `CString`, `CMap`, etc.). Those classes don't know, that they should serialize their data as XML, and boost doesn't know them either, so things will break. What's worse, changing the binary format will leave your customers with unrecoverable serialized streams (i.e. after upgrading, their old data can no longer be read). A better alternative would be to keep your MFC-serialized streams, wrap the logic in a C++/CLI module, and use that from your C# code. – IInspectable May 30 '16 at 18:30
  • All the old serialized data is available in DB so customer is not loosing any data. i know there are better alternatives for implementing serialization. Issue/priority we have here is "time", Can you suggest any other options. writing a c++ wrapper will be time consuming as we have 4 modules to be wrapped in less than a month. Thanks. – bhardwajhp May 31 '16 at 07:40
  • Writing a C++/CLI module that contains all the unmanaged classes to be serialized, with wrappers for 20 classes should be done in less than a week. This is both the solution with the least risk of breaking things (as it's reusing what has been tested and shipped), and certainly no more time-consuming than rewriting all the serialization in .NET, in addition to implementing new classes. C++/CLI is the de-facto standard way to make unmanaged code available to .NET languages. If time and risk are driving factors, that's what you need. – IInspectable May 31 '16 at 07:46
  • Thanks for your inputs. What do you think about : "http://www.codeproject.com/KB/library/wfc/CXMLArchive.html" – bhardwajhp Jun 01 '16 at 16:23
  • Not very helpful. This is not a drop-in replacement for the `CArchive` class. It's just a custom implementation, that allegedly implements object persistence. And fails to do so. In contrast to MFC, this implementation cannot construct objects from the serialized stream. The user will have to construct them, and stream serialized data back in. In all, a fairly ragged, amateurish implementation. Recommendation is still: Keep everything as it is, and wrap it up in a C++/CLI module for consumption by your favorite .NET language. The only course that allows you to perform regression testing. – IInspectable Jun 01 '16 at 22:53
  • Thanks IInspectable. I did not know there is language called "C++/CLI". Looks easier for implementation as i know C++. Thanks for showing me the right direction here. – bhardwajhp Jun 02 '16 at 14:45
  • @IInspectable, can you please share any good reference for understanding C++/CLR. i found ways to covert CSTring to String (C#) but not the other way around. also any good references which can help me translate Ptr array or ptrlist classes of vc++. i have found many references for standard c++ concersion but not any good references for same in VC++. Thanks – bhardwajhp Jun 05 '16 at 12:52
  • The standard solution is to write a C++/CLI `ref class`, that has a pointer to the unmanaged object as its only member, and provide getters/setters that perform the conversion, and forward any methods. Links: [Quick C++/CLI - Learn C++/CLI in less than 10 minutes](http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-minutes), [.NET Programming with C++/CLI](https://msdn.microsoft.com/en-us/library/68td296t.aspx), [C++/CLI in Action](http://www.amazon.com/dp/1932394818). Particularly the book is very helpful, and a quick read, too. – IInspectable Jun 06 '16 at 09:51
  • Thanks Again. I have wrapped most of the classes involved. but there is some code which is not part of classes. Do we need to wrap the old scattered code in class which we can write wrapper for. or is there any other method available for accessing that code. – bhardwajhp Jun 09 '16 at 12:59
  • I believe there is no way to express free functions in .NET. If you want those free functions to be called from managed code you will have to wrap them up in a class as static class members (or P/Invoke into native exports). – IInspectable Jun 09 '16 at 13:18
  • Old application is derived from CWinApp and couple of its features are used as part of project like AfxSocketInit. Please suggest how we can implement this in DLL. Thanks. – bhardwajhp Jun 14 '16 at 14:47
  • @IInspectable Can you share some information on how to add CWinapp derived classes into C++/CLi project – bhardwajhp Jun 16 '16 at 15:16
  • Breaking the dependency on the `CWinApp`-derived implementation is a bit of a challenge. It is possible (I did this once to allow isolated testing of serializable `CObject`-derived classes). But it won't be easy, and it won't be pretty either. I don't recall the details either, however, as a general guideline you could start by identifying the features of `CWinApp` that you need, and replicate/fake those. It is fairly involved, and cannot be addressed in a comment. You should probably ask a dedicated question on how to get MFC serialization going without a `CWinApp`-derived class. – IInspectable Jun 16 '16 at 17:13
  • Thanks, @IInspectable Will do. Meanwhile any tips on reading/writing data to CDWordArray. i was able to read data from CtypedPtrArray, but i am kind of blocked with DWord Array. – bhardwajhp Jun 21 '16 at 11:39
  • added new question for more details "http://stackoverflow.com/questions/37943657/how-to-get-mfc-serialization-going-without-a-cwinapp-derived-class" – bhardwajhp Jun 21 '16 at 12:03

0 Answers0