473,836 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using managed c++ ptr from unmanaged c++

I want to use a managed c++ class from an unmanaged class. Here is my
code:

// *** Unmanaged Code

// .h file
class UnmanagedClass
{
public:
// Other stuff here
private:

std::string mMessage;
nogc_ManagedCla ss* mNogc_ManagedCl ass;
};
// .cpp file
UnmanagedClass: :UnmanagedClass (void)
{
mNogc_ManagedCl ass = nogc_ManagedCla ss::create();
}

// *** Managed Code

// .h file
class nogc_ManagedCla ss
{
public:

// some other non static stuff
static nogc_ManagedCla ss* create(void);
};
// .cpp file

#using <mscorlib.dll >
#include ".\nogc_Managed Class.h"

nogc_ManagedCla ss* nogc_ManagedCla ss::create(void )
{
return new nogc_ManagedCla ss();
}

I am fairly new to managed c++. I have seen some posting suggesting
that the pointer to the managed class should be wrapped in gcroot<T>,
but I think this is only true if the managed class is marked as __gc.
Is this a safe way to create and use a managed class from an unmanaged
class?
Nov 17 '05 #1
5 1735
Andrew,
I want to use a managed c++ class from an unmanaged class. Here is my
code:

// *** Unmanaged Code

// .h file
class UnmanagedClass
{
public:
// Other stuff here
private:

std::string mMessage;
nogc_ManagedCla ss* mNogc_ManagedCl ass;
};
// .cpp file
UnmanagedClass: :UnmanagedClass (void)
{
mNogc_ManagedCl ass = nogc_ManagedCla ss::create();
}

// *** Managed Code

// .h file
class nogc_ManagedCla ss
{
public:

// some other non static stuff
static nogc_ManagedCla ss* create(void);
};
// .cpp file

#using <mscorlib.dll >
#include ".\nogc_Managed Class.h"

nogc_ManagedCla ss* nogc_ManagedCla ss::create(void )
{
return new nogc_ManagedCla ss();
}

I am fairly new to managed c++. I have seen some posting suggesting
that the pointer to the managed class should be wrapped in gcroot<T>,
but I think this is only true if the managed class is marked as __gc.
A Managed class *is* marked as __gc. Otherwise you've got an unmananged
class. I think you're confusing what a managed class is with an unmanaged
class that happens to have managed methods. Two entirely different things.

In you're case, it seems you're only dealing with __nogc (unmanaged)
classes, so you should be fine, in general (basically, the usual C++ rules
should apply in these cases).

--
Tomas Restrepo
to****@mvps.org
Is this a safe way to create and use a managed class from an unmanaged
class?

Nov 17 '05 #2
"Tomas Restrepo \(MVP\)" <to****@mvps.or g> wrote in message news:<OB******* *******@TK2MSFT NGP11.phx.gbl>. ..
Andrew,

A Managed class *is* marked as __gc. Otherwise you've got an unmananged
class. I think you're confusing what a managed class is with an unmanaged
class that happens to have managed methods. Two entirely different things.

In you're case, it seems you're only dealing with __nogc (unmanaged)
classes, so you should be fine, in general (basically, the usual C++ rules
should apply in these cases).

Thanks for you response Tomas.

For some reason I was under the impression that a managed class was a
class that was compiled with the /CLR options and #using
<mscorlib.dll >. I have a couple more questions:

1. So will the "nogc_ManagedCl ass" that I had in the example compile
to CIL, and the "UnmanagedClass " complile to x86 code?

2. If I want to use a __gc class from my "nogc_ManagedCl ass" I will
need to have the pointer to that class wrapped with gcroot<T>?
Nov 17 '05 #3
Hi Andrew,
Thanks for you response Tomas.

For some reason I was under the impression that a managed class was a
class that was compiled with the /CLR options and #using
<mscorlib.dll >. I have a couple more questions:

1. So will the "nogc_ManagedCl ass" that I had in the example compile
to CIL, and the "UnmanagedClass " complile to x86 code?
A class does not compile to code. *Methods* in the class, will, though.

That's the key point. MC++ makes a very explicit exception about what is
managed data (i.e. __gc/__nogc classes) and what is managed code (methods
that compile to CIL or x86).

So you can have a __nogc class whose *methods* are compiled to CIL, but the
class itself is still unmanaged (meaning it gets allocated in the stack or
the unmananged heap), or __nogc classes that contain both managed and
unmanaged methods. A __gc class, on the other hand, only has managed
methods.

So, when you compile code with /clr, classes by default remain __nogc, but
the methods they contain might be compiled as managed code. That's perfectly
valid.
2. If I want to use a __gc class from my "nogc_ManagedCl ass" I will
need to have the pointer to that class wrapped with gcroot<T>?


Depends on what you mean by "use". If you mean contain a reference to the
__gc class as a field of your __nogc class, then the answer is yes. However,
any managed methods the __nogc class have can manipulate a __gc class
directly, without gcroot.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4
"Tomas Restrepo \(MVP\)" <to****@mvps.or g> wrote in message news:<u9******* *******@tk2msft ngp13.phx.gbl>. ..
Hi Andrew,
Thanks for you response Tomas.

For some reason I was under the impression that a managed class was a
class that was compiled with the /CLR options and #using
<mscorlib.dll >. I have a couple more questions:

1. So will the "nogc_ManagedCl ass" that I had in the example compile
to CIL, and the "UnmanagedClass " complile to x86 code?


A class does not compile to code. *Methods* in the class, will, though.

That's the key point. MC++ makes a very explicit exception about what is
managed data (i.e. __gc/__nogc classes) and what is managed code (methods
that compile to CIL or x86).

So you can have a __nogc class whose *methods* are compiled to CIL, but the
class itself is still unmanaged (meaning it gets allocated in the stack or
the unmananged heap), or __nogc classes that contain both managed and
unmanaged methods. A __gc class, on the other hand, only has managed
methods.

So, when you compile code with /clr, classes by default remain __nogc, but
the methods they contain might be compiled as managed code. That's perfectly
valid.
2. If I want to use a __gc class from my "nogc_ManagedCl ass" I will
need to have the pointer to that class wrapped with gcroot<T>?


Depends on what you mean by "use". If you mean contain a reference to the
__gc class as a field of your __nogc class, then the answer is yes. However,
any managed methods the __nogc class have can manipulate a __gc class
directly, without gcroot.


Tomas,

I am working a project that is currently written in c++. I need to
modify this project so that it uses some components written in c#.
This modification needs to be done with as little effect in the
existing code as possible. I am considering have my current c++ code
have a pointer to a nogc class and the nogc class have a pointer to a
gc class. This gc class will then communicate with some c#
assemblies.

Is there any value in only including the /clr compile option on the
nogc and gc classes and leaving it off the rest of the classes in the
project? Based on your last response, it seems that including the
/clr option on the entire project should have little effect on the old
c++ classes.

If I include the /clr option on either a subset of classes or the
entire project what effect does this have on linking? Can incremental
links still be done? I heard from a co-worker that adding the /clr
option to a project means incremental links can no longer be done.
This is a large project, so this could slow down development
considerably.
Nov 17 '05 #5
Hi Andrew,

I am working a project that is currently written in c++. I need to
modify this project so that it uses some components written in c#.
This modification needs to be done with as little effect in the
existing code as possible. I am considering have my current c++ code
have a pointer to a nogc class and the nogc class have a pointer to a
gc class. This gc class will then communicate with some c#
assemblies.

Is there any value in only including the /clr compile option on the
nogc and gc classes and leaving it off the rest of the classes in the
project? Based on your last response, it seems that including the
/clr option on the entire project should have little effect on the old
c++ classes.
It might have little impact on the source code, but it might have an impact
on performance, in some cases (depends a lot on what your code is like, so
it's not easy to say upfront). I suggest you do a few tests to see what
works best for you.
If I include the /clr option on either a subset of classes or the
entire project what effect does this have on linking? Can incremental
links still be done? I heard from a co-worker that adding the /clr
option to a project means incremental links can no longer be done.
This is a large project, so this could slow down development
considerably.


If this is an issue, then I'd recommend moving the code you're compiling
with /clr to a different module (i.e. isolate it to it's own DLL). That way,
it would be easier to maintain. In general, in cases like this, keeping the
code isolated will mean the minimal impact on your compiling process.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #6

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

Similar topics

47
3549
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); It is meant to be called by C, but I would rather use it from C#. So I wrote the following C# program to test it, but it failed to work. The call to compress doesn't return or throw an exception, it simply...
1
2130
by: Eric Twietmeyer | last post by:
Hello, I'm starting to investigate cs, managed c++ and interoperating with a very large unmanaged code base. We are going to use Windows Forms (written in cs) to replace our old fashioned GUI. The guts will remain in unmanaged c++. So I need to write public managed c++ wrapper classes for the couple of interfaces the unmanaged stuff needs to expose for the GUI. Questions:
5
3632
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However, I need .NET to instantiate this object and somehow allow the this object to callback to .NET. So, my question is: 1. How can a managed class instantiate and unmanaged class?
8
3619
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my C# program. When I look at the reference and I double click to view it in the objec browser , I see only structs , without their members. And I don't see methods.
3
2221
by: mirek | last post by:
Hi, I've got problem building managed class library to wrap unmanaged code. I created managed class library using standard patten: #include "../Unmanaged/Class1.h" //Class1 unmanaged namespace ManagedWrappers { public __gc MClass1
1
2264
by: RaKKeR | last post by:
Hi, I'm trying to wrap my unmanaged c++ code into a managed c++ wrapper. The problem is VC .NET doesn't seem to find the namespaces defined in my unmanaged code. Let's say I have a file "server.h" with a namespace server. When I include this file in another file that will be compiled as managed, the compiler gives me the following error: C2871: 'server' : a namespace with this name does not exist
3
2785
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple question.
12
12561
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley retreiving the data. Presently all of the c++ code is still in .NET 1.1, so we're using a _nogc bridge class wrapped in a _gc c++ class in order to facilitate this interop. But we've converted everything not c++ to .NET 2.0 and would love to
25
3023
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class MyClass:IDisposable
0
9825
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10559
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10260
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5652
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4460
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.