I received the following e-mail recently:
"I've been searching the Net and documentation for how to use a WinForms control from an MFC app. So far, nothing. Is it possible? We have a big old MFC-based doc/view app, and need to add some significant new functionality. I'd like to write new code in .NET but can't afford to rewrite the whole UI."As more people move to .NET, theyre going to want to write more and more of their code as managed to take advantage of the productivity gains of that style of programming. However, as much as they might like to, these same people are not often going to want to move their existing code. One popular scenario pointed out here is the need to use .NET code from an existing MFC application. This scenario is supported directly by the variant of C++ supplied by Microsoft in Visual Studio .NET called Managed C++ (sometimes also called the Managed Extensions to C++).
The goal with MC++ is to allow existing C++ code to bring in and expose managed types; i.e., types compatible with .NET. To do this, you need to follow several steps:
- If you havent already, load your MFC project into VS.NET and let the environment port your project settings into its new format.
- In the Solution Explorer, right click on your MFC project and choose Properties.
- On the General page, set the Use Managed Extensions option to True, which will compile your application with the /clr switch. This is the magic that will let you consume and implement managed types. In fact, on your next compile, your MFC application will be managed code (except where it cant be for backwards compatibility purposes).
- Before pressing OK to close your project Property Pages, navigate to the Configuration Properties>C/C++>General page and set the Debug information Format to Program Database (/Zi). Unfortunately, .NET does not yet support Edit & Continue.
- Also, navigate to Configuration Properties>C/C++>Code Generation and set Enable Minimum Rebuild to No. This setting is incompatible with the /clr switch.
- Finally, on the same page, change Basic Runtime Checks to Default. The /RTC1 switch (stack frame checking and uninitialized variable checking) is also incompatible with the /clr switch.
- Finally, you can press OK and build your project and run it.
In complicated projects, you may have some other compiler errors to deal with, but once youve got those under control, you can now run your application. It should look and act identically. The only difference is that its managed instead of unmanaged. However, none of the types are managed, for example, the classes that make up your application, which is why you still write your code in the exact same way. The only noticeable difference is that you can now pull in managed types. For example, to show the managed MessageBox, do the following:
- Somewhere in your code, such as in your CWinApps OnAppAbout message,
type the following code:
System::Windows::Forms::MessageBox::Show(S"Ain't .NET cool?");
- To make this code work, at the top of the file with this code, add the following
lines to bring in the managed WinForms library (where MessageBox lives) and
to turn off the MessageBox macro:
#using <System.Windows.Forms.dll> #undef MessageBox
- Compile and run, invoking your code to show the message box.
- Bask in the magic of IJW (It Just Works) that the VC++ compiler team needed to perform to get all of your unmanaged types to work in managed code with managed types.:
Of course, thats not even the tip of what Managed C++ can do. For the
full story, I recommend Visual
Studio .NET: Managed Extensions Bring .NET CLR Support to C++, MSDN
Magazine, July 2001 (by me : ).
Chris Sells in an independent consultant specializing in distributed applications in .NET and COM, as well as an instructor for DevelopMentor. He's written several books including ATL Internals, which is currently being updated for ATL7. He's also working on Essential Windows Forms for Addison-Wesley and Mastering Visual Studio .NET for O'Reilly. In his free time, Chris hosts the Web Services DevCon (November 2002) and directs the Genghis source-available project. More information about Chris and his various projects is available at http://www.sellsbrothers.com.
Chris is hosting the Web Services DevCon October 1011, 2002 in Bedford, Massachussetts (Greater Boston). Sign up now for the Web Services DevCon to see industry experts like Don Box, Sam Ruby, and Tim Ewald show you how Web Services are going to change the way you build your systems. You'll also hear from Microsoft's Web Services Program Manager, Keith Ballinger, and IBM Distinguished Engineer, Noah Mendelsohn, as well as wizened practitioners from HP, Macromedia, and the W3C. Details and online registration at http://www.sellsbrothers.com/conference.