August 06, 2002
Using Managed Code from MFCChris Sells
Update your legacy MFC apps in .NET without rewiting your older code by using Managed C++. Managed C++ allows existing C++ code to bring in and expose managed types that are compatible with .NET.
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:
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:
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.
|
|
||||||||||||||||||||||||||||
|
|
![]() |
||||
![]() |
|
|||
![]() |
||||
![]() |
||||
![]() |
|
|||
![]() |
|
|||