-
July 27th, 2009, 10:23 AM
#1
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
-
July 27th, 2009, 03:49 PM
#2
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
-
July 28th, 2009, 12:24 AM
#3
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.
-
July 28th, 2009, 02:15 AM
#4
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.
-
July 28th, 2009, 02:15 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|