CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Posts
    98

    Mixing managed/unmanaged classes

    Hello,

    Until now, I have been writing either native console applications in C++, or .NET Windows Forms applications in C#. I now want to start creating a user interface for my C++ programs, and so I have started trying out C++ with Windows Forms. I use Visual Studio 2008.

    So, I started an empty CLR project. The project has a Windows Form, Form1, and a class to act as an "engine" for the whole program, called Engine. Engine contains an instance of Form1. In my main loop, I created an instance of Engine - which in turn should create an instance of Form1 - but it failed to compile, telling me that "mixed types are not supported".

    It seemed to me that Form1 is, by default, managed by the garbage collector, and as such cannot be a member of a native class.

    So, to try to solve this, I started another project, using the Windows Forms project wizard. This automatically loads up a form in the main loop. I gave the Form an instance of the Engine class as a member variable. However, on running the program, I again was told that "mixed types are not supported".

    Am I right in thinking that, if I am writing a .NET program using Windows Forms, then all of my classes - included the user-defined classes - also need to be managed by .NET?

    It looks I need to specify that my user classes should be managed by the garbage collector, rather than by myself....would this work? And how can I do this?

    Thanks

    EJ

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mixing managed/unmanaged classes

    CLR., .NET, and Windows Forms questions do not belong in this forum. They belong in the various forums dedicated to .NET programming.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Mixing managed/unmanaged classes

    To keep unmanaged instance inside managed, use pointer instead of instance.
    To keep managed instance inside unmanaged, use gcroot keyword.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Mixing managed/unmanaged classes

    You can have managed types inside a native type, but you must use gcroot<> template, and build with /clr support.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Mixing managed/unmanaged classes

    [ redirected & merged ]

    Please don't create multiple threads in different forums for the same topic. If the question is misplaced ask a moderator to move it.
    Last edited by cilu; July 28th, 2009 at 02:17 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured