Managed referencing non-managed and vice-versa

  • Thread starter =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
  • Start date
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Hello.

I am developing a project in managed C++ for the first time. Things were
going fine up until the point I had the need to mix managed and
non-managed code. More specifically, what I am trying to have is a
MVC-like scheme, where the view is implemented using Windows Forms and
the model is composed of a bunch of previously-written non-managed C++
classes. That implies that the view (managed entity) will hold a
reference to the model (non-managed entity) and vice-versa, which the
compiler doesn't seem to like. What is the workaround for that? Must I
make all my model classes managed ones? What if that were not an option?

Besides being a Visual C++ .NET newbie, I have been programming for the
last 10 hours and could easily be missing something very basic, so
please bear with me.

Thank you very much,
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Hi.

I wonder whether there is an easy solution to this problem, because I am
really stuck. Trying to store a handle in a non-managed class is not
possible:

public ref class Form1 : public System::Windows::Forms::Form
{
// ...
};

class Model
{
private:
Form1^ theView; // Error: cannot store a handle here
};

I thought about using a non-member function to make the bridge between
the non-managed and managed classes, but that also did not work. Could
anyone please give me a hand with this (rather common?) problem?

Thank you,
 
I

Ioannis Vranos

Ney said:
Hi.

I wonder whether there is an easy solution to this problem, because I am
really stuck. Trying to store a handle in a non-managed class is not
possible:

public ref class Form1 : public System::Windows::Forms::Form
{
// ...
};

class Model
{
private:
Form1^ theView; // Error: cannot store a handle here
};

I thought about using a non-member function to make the bridge between
the non-managed and managed classes, but that also did not work. Could
anyone please give me a hand with this (rather common?) problem?


Talking about VS 2003 which is the latest official product out there, you can place
unmanaged pointers in managed classes (and create unmanaged objects using 'new' in
constructor and 'delete' in the destructor), and place managed pointers ("handles" in
upcoming VS 2005) in unmanaged classes by using gcroot.
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Ioannis said:
Talking about VS 2003 which is the latest official product out there,
you can place unmanaged pointers in managed classes (and create
unmanaged objects using 'new' in constructor and 'delete' in the
destructor), and place managed pointers ("handles" in upcoming VS 2005)
in unmanaged classes by using gcroot.

Thank you all for your responses. I swear I couldn't find any mention of
gcroot (at least not one that rang a bell) while I was searching for a
solution. Or maybe it was just my exhaustion.

Anyway, I ended up using a kludge: I built a ref class to act as a
mediator. That mediator uses a static member variable to hold a handle
to the managed object I was interested in and a static member variable
to allow my non-managed entity to indirectly access it. That worked, but
I will look up "gcroot" as it seems like a cleaner solution.

Cheers,
 
I

Ioannis Vranos

Ney said:
Thank you all for your responses. I swear I couldn't find any mention of
gcroot (at least not one that rang a bell) while I was searching for a
solution. Or maybe it was just my exhaustion.

Anyway, I ended up using a kludge: I built a ref class to act as a
mediator. That mediator uses a static member variable to hold a handle
to the managed object I was interested in and a static member variable
to allow my non-managed entity to indirectly access it. That worked, but
I will look up "gcroot" as it seems like a cleaner solution.


An example:


#include <vector>

// For gcroot
#include <vcclr.h>


using namespace std;
using namespace System;


vector< gcroot<Managed_Class *> > vec;

like

vector< gcroot<Button *> >vec;



This is with "managed extensions" syntax (managed pointers replaced by "handles" in VD 2005).


However in VS 2005, there will be "STL .NET" (an STL implementation working with managed
objects), making gcroot redundant when using that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top