I must solve two problems:
- use an MFC property sheet from a C# app
- manage ODBC and ADO.NET connections in a single exe
- An Overview of Managed/Unmanaged Code Interoperability
- MFC under the hood
- Native and .NET interoperability
- Using C++ Interop (Implicit PInvoke)
- Introduction to managed C++
- Intro to Managed C++, Part 2: Mixing Managed and Unmanaged Code
- C++/CLI Migration Primer
- Mixed (Native and Managed) Assemblies
- How to: Compile MFC and ATL Code with /clr
- Moving to .NET and WinFX: A Roadmap for C/C++ Applications
- Extending MFC Applications with the .NET Framework (Tom Archer)
- Subclass System.Windows.Forms.Control
The Winforms C# App calls a C++ managed wrapper (derived from System::Windows::Forms::Control). Upon creation of its handle, the wrapper associates the handle with a CWnd-derived MFC class. The original code from the Codeproject article used C++ managed extensions. I compiled it with VS2005 so I had to translate it from C++ managed extensions to C++/CLI and it worked just fine.
This technique works ok with a CWnd-derived class. Unfortunately our legacy MFC code is based on property sheets so this is not going to be enough, but it’s a start.
- One single C++/CLI App containing MFC legacy source and dialog templates.
However for some reason only the Release version of the executable worked. The Debug version kept displaying the following assertion,
an instead of displaying the appropriate line of code just got my machine to thrash.
Anyway this is still not quite what I want to do: my Windows Forms MDI framework is written in C#, not in C++. The legacy code must reside a DLL compiled in mixed mode.
- A C# Windows Forms project calls a dialog residing inside a mixed-mode exe
I created the MFC dialog project with the Wizard then modified its properties:
- Changed to DLL
- Switched on /clr
- Disabled pre-compiled headers.
- Same as above but using Alexey Shalnov’s MFCAppAdapter class
- Created an MFC MDI App with Visual Studio 2005.
- Added MFCAppAdapter.cpp (copied from Alexey Shalnov’s article).
- Changed MFCAppAdapter.cpp compile options to /clr (at the file level, not the project level).
- Set Code Generation option: set to default.
- Set Enable Minimal Rebuild option to No.
- Set Enable C++ Exceptions to Yes with SEH
- Set Debug Information Format to Zi
- Add references to System and System.Windows.Forms