MFC and .NET Explorations

19 November 2006

Mixing legacy MFC code with Windows Forms (Part 2)

I know how to call unmanaged code from a managed class.

How to do the opposite?

This piece of code

    public class DiagramContainer
    {
 
    private:
        // Managed reference to some data
        Rigging^ m_rigging;
    };

returns the following error:

Error 1 error C3265: cannot declare a managed 'm_rigging' in an unmanaged 'MFCControls::DiagramContainer'

Hints:
http://www.ondotnet.com/pub/a/dotnet/2003/03/03/mcppp2.html?page=last

Solution:
Use gcroot

    public class DiagramContainer 
    {
    private:
        // Managed reference to some data. gcroot makes it look like an unmanaged type
        gcroot<rigging^ > m_rigging;
    };


m_rigging can be used as a usual Rigging^ object. You reach the members using ->