473,505 Members | 13,696 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_ManagedClass* mNogc_ManagedClass;
};
// .cpp file
UnmanagedClass::UnmanagedClass(void)
{
mNogc_ManagedClass = nogc_ManagedClass::create();
}

// *** Managed Code

// .h file
class nogc_ManagedClass
{
public:

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

#using <mscorlib.dll>
#include ".\nogc_ManagedClass.h"

nogc_ManagedClass* nogc_ManagedClass::create(void)
{
return new nogc_ManagedClass();
}

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 1718
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_ManagedClass* mNogc_ManagedClass;
};
// .cpp file
UnmanagedClass::UnmanagedClass(void)
{
mNogc_ManagedClass = nogc_ManagedClass::create();
}

// *** Managed Code

// .h file
class nogc_ManagedClass
{
public:

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

#using <mscorlib.dll>
#include ".\nogc_ManagedClass.h"

nogc_ManagedClass* nogc_ManagedClass::create(void)
{
return new nogc_ManagedClass();
}

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.org> wrote in message news:<OB**************@TK2MSFTNGP11.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_ManagedClass" 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_ManagedClass" 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_ManagedClass" 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_ManagedClass" 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.org> wrote in message news:<u9**************@tk2msftngp13.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_ManagedClass" 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_ManagedClass" 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
3479
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,...
1
2102
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. ...
5
3609
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,...
8
3601
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...
3
2195
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 ...
1
2246
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...
3
2755
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...
12
12499
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...
25
2978
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...
0
7098
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
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
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...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5026
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...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
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...

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.