473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Managed referencing non-managed and vice-versa

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,

--
Ney André de Mello Zunino
Nov 17 '05 #1
5 1557
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,

--
Ney André de Mello Zunino
Nov 17 '05 #2
Hi,
Look in MSDN on gcroot template.
Hope it will help you,
Boni
"Ney André de Mello Zunino" <zu****@inf.ufsc.br> schrieb im Newsbeitrag
news:e$**************@TK2MSFTNGP12.phx.gbl...
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,

--
Ney André de Mello Zunino

Nov 17 '05 #3
Ney André de Mello Zunino wrote:
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.
Nov 17 '05 #4
Ioannis Vranos wrote:
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,

--
Ney André de Mello Zunino
Nov 17 '05 #5
Ney André de Mello Zunino wrote:
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.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ian | last post by:
Hi All I have a DLL (nahd.dll) that has been supplied to me will a PBX phone system. According to the documentation it is to allow developers to write there own applications to monitor the...
3
by: MattB | last post by:
Is there any way to reference a cell in a datagrid's ItemDataBound event by it's DataItem column name (e.item.DataItem("colname")). I need to set a cell's text to "" based on the contents of it's...
6
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the...
2
by: Young J. Putt | last post by:
I've got a list box bound to a Datatable, like this: lstProjects.DataSource = m_oProjectSet.DataTable lstProjects.DisplayMember = "ProjectDesc" lstProjects.ValueMember = "ProjectID" I want to...
9
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of...
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
1
by: Earl | last post by:
I have some crappy Intuit sample code that I'm trying to clean up and turn into something useful. They reference a non-CLS compliant Interop class. Do I have any options besides marking the entire...
3
by: Torsten Wiebesiek | last post by:
Hi folks, currently I'm writing image classes for easier handling of Intel's IPP library. My idea is to have to different classes. One class that represents a complete image and deals with all...
1
by: Dave Anson | last post by:
What is the recommended practice for referencing assemblies in a project from other solutions? I am using Visual Studio 2005 Team System. I have several assemblies in another solution which will...
28
by: rahul | last post by:
#include <stdio.h> int main (void) { char *p = NULL; printf ("%c\n", *p); return 0; } This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.