Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Using Managed Code from MFC


Using Managed Code from MFC

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, they’re 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:

  1. If you haven’t already, load your MFC project into VS.NET and let the environment port your project settings into its new format.
  2. In the Solution Explorer, right click on your MFC project and choose Properties.
  3. 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 can’t be for backwards compatibility purposes).
  4. 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.
  5. Also, navigate to Configuration Properties>C/C++>Code Generation and set Enable Minimum Rebuild to No. This setting is incompatible with the /clr switch.
  6. 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.
  7. 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 you’ve got those under control, you can now run your application. It should look and act identically. The only difference is that it’s 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:

  1. Somewhere in your code, such as in your CWinApp’s OnAppAbout message, type the following code:
    System::Windows::Forms::MessageBox::Show(S"Ain't .NET cool?");
  2. 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
  3. Compile and run, invoking your code to show the message box.
  4. 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, that’s 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 10–11, 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.



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.