Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

Managed referencing non-managed and vice-versa

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ney André de Mello Zunino - 03 May 2005 09:13 GMT
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,

Signature

Ney André de Mello Zunino

Ney André de Mello Zunino - 04 May 2005 12:38 GMT
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,

Signature

Ney André de Mello Zunino

Boni - 04 May 2005 13:47 GMT
Hi,
Look in MSDN on gcroot template.
Hope it will help you,
Boni
> Hi.
>
[quoted text clipped - 18 lines]
>
> Thank you,
Ioannis Vranos - 04 May 2005 14:04 GMT
> Hi.
>
[quoted text clipped - 16 lines]
> 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.
Ney André de Mello Zunino - 06 May 2005 01:34 GMT
> 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,

Signature

Ney André de Mello Zunino

Ioannis Vranos - 06 May 2005 18:31 GMT
> 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
[quoted text clipped - 5 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.