473,386 Members | 1,694 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.

Mixed managed and unmanaged code - How to do it?

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.

1) I have two classes defined in two header files, it is possible to have a
member from the other class? My problem is that as soon as I include the
other header file I get linking errors. See example below.

2) It is possible to call from unmanaged code a managed instance of a class?
I can use GCHandle class to extract a fixed pointer to the managed class but
how do I cast this pointer in my unmanaged class to a "managed" class
instance? Or it is better to use the template gcroot<ManagedClass*>?

Thanks for any help!

Cheers

Thorsten

Example Code:

-> Managed.h

class CUnmanaged;

#pragma once

#include "Unmanaged.h"

namespace Test

{

public __gc class CManaged

{

private:

CUnmanaged* m_Unmanaged;

public:

CManaged();

void DoSomeThing();

};

}

-> Unmanaged.h

class Test::CManaged;

#pragma once

#include "Managed.h"

#include <vcclr.h>

__nogc class CUnmanaged

{

public:

gcroot<Test::CManaged*> m_Managed;

void DoSomeThing();

CUnmanaged();

};

Some errors are like this:

Unmanaged.h error C2653: 'Test' : is not a class or namespace name

Unmanaged.h error C2079: 'CManaged' uses undefined class 'Test'

Unmanaged.h(12): error C2059: syntax error :
Nov 17 '05 #1
3 2746
> 1) I have two classes defined in two header files, it is possible to have
a member from the other class? My problem is that as soon as I include the
other header file I get linking errors. See example below.
If you are just declaring pointers of a class, there is no need to include
it. A forward reference is enough:
<code>
class CUnmanaged;

#pragma once

// #include "Unmanaged.h" <- no need to include unmanaged.h here

namespace Test
{
public __gc class CManaged
{
CUnmanaged* m_Unmanaged;
public:
CManaged();
void DoSomeThing();
};
}
</code>
2) It is possible to call from unmanaged code a managed instance of a
class? I can use GCHandle class to extract a fixed pointer to the managed
class but how do I cast this pointer in my unmanaged class to a "managed"
class instance? Or it is better to use the template gcroot<ManagedClass*>?


Neither GCHandle nor gcroot can be used to call from unmanaged code to an
instance of a managed class. They allow you to have a member variable of an
umanaged class that refers to an instance of a managed object.

To achieve what you want, have a look at other recent threads [1], [2] in
this newsgroup for details.

[1]http://groups.google.com/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/6014ee0a87a1a61b/1da4e633322dda9d#1da4e633322dda9d
[2]
http://groups.google.com/group/micro...439de50486887f
Nov 17 '05 #2
HI Marcus

thanks for your help!

But the thing with gcroot<> actually works. I have a Class like this:
__nogc class CSampleGrabberCallback : public ISampleGrabberCB
{
public:
gcroot< DirectX::TK::Video*> m_pVideo;
}

After I set the m_pVideo variable I can call out of the
CSampleGrabberCallback any function out of the managed class Video.

Cheers
Thorsten

<- C# is heaven compare to managed/unmanaged C++ ;-) ->

"Marcus Heege" <NO****@heege.net> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
1) I have two classes defined in two header files, it is possible to have
a member from the other class? My problem is that as soon as I include
the other header file I get linking errors. See example below.


If you are just declaring pointers of a class, there is no need to include
it. A forward reference is enough:
<code>
class CUnmanaged;

#pragma once

// #include "Unmanaged.h" <- no need to include unmanaged.h here

namespace Test
{
public __gc class CManaged
{
CUnmanaged* m_Unmanaged;
public:
CManaged();
void DoSomeThing();
};
}
</code>
2) It is possible to call from unmanaged code a managed instance of a
class? I can use GCHandle class to extract a fixed pointer to the managed
class but how do I cast this pointer in my unmanaged class to a "managed"
class instance? Or it is better to use the template
gcroot<ManagedClass*>?


Neither GCHandle nor gcroot can be used to call from unmanaged code to an
instance of a managed class. They allow you to have a member variable of
an umanaged class that refers to an instance of a managed object.

To achieve what you want, have a look at other recent threads [1], [2] in
this newsgroup for details.

[1]http://groups.google.com/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/6014ee0a87a1a61b/1da4e633322dda9d#1da4e633322dda9d
[2]
http://groups.google.com/group/micro...439de50486887f

Nov 17 '05 #3
Just to make the difference clear:

If I understood you right, your initial question was whether it is possible
to use gcroot to call a managed object from unmanaged code.

If this was your initial question, then the answer is still no.

Your observation is a different one: You are calling a managed object from a
managed function of an unmanaged class. So in the end you are calling a
managed object from managed code.

Marcus
"Thorsten" <TK***@gmx.de> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
HI Marcus

thanks for your help!

But the thing with gcroot<> actually works. I have a Class like this:
__nogc class CSampleGrabberCallback : public ISampleGrabberCB
{
public:
gcroot< DirectX::TK::Video*> m_pVideo;
}

After I set the m_pVideo variable I can call out of the
CSampleGrabberCallback any function out of the managed class Video.

Cheers
Thorsten

<- C# is heaven compare to managed/unmanaged C++ ;-) ->

"Marcus Heege" <NO****@heege.net> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
1) I have two classes defined in two header files, it is possible to
have a member from the other class? My problem is that as soon as I
include the other header file I get linking errors. See example below.


If you are just declaring pointers of a class, there is no need to
include it. A forward reference is enough:
<code>
class CUnmanaged;

#pragma once

// #include "Unmanaged.h" <- no need to include unmanaged.h here

namespace Test
{
public __gc class CManaged
{
CUnmanaged* m_Unmanaged;
public:
CManaged();
void DoSomeThing();
};
}
</code>
2) It is possible to call from unmanaged code a managed instance of a
class? I can use GCHandle class to extract a fixed pointer to the
managed class but how do I cast this pointer in my unmanaged class to a
"managed" class instance? Or it is better to use the template
gcroot<ManagedClass*>?


Neither GCHandle nor gcroot can be used to call from unmanaged code to an
instance of a managed class. They allow you to have a member variable of
an umanaged class that refers to an instance of a managed object.

To achieve what you want, have a look at other recent threads [1], [2] in
this newsgroup for details.

[1]http://groups.google.com/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/6014ee0a87a1a61b/1da4e633322dda9d#1da4e633322dda9d
[2]
http://groups.google.com/group/micro...439de50486887f


Nov 17 '05 #4

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

Similar topics

5
by: _BNC | last post by:
I've been adapting an older unmanaged C++ legacy app to C#---with limited success. The original app made use of an older, but straightforward, C DLL that worked efficiently under the VC++ 6 model....
2
by: Shawn B. | last post by:
Greetings, I'm new to MC++ (but not C#) so bear with me. I have a question... I have created a 6502/65c02/65816 CPU processor core for a simulator (in C#) and now for a part of performance...
4
by: Droopy | last post by:
Hi, I wrote a C# program that is calling C++ legacy code. I wrote a C++ managed wrapper for this legacy code. It seems to work well. In Debug mode, I had a linker error LNK4210 that I solved...
3
by: JoeProgrammer | last post by:
A couple of questions re. managed vs. unmanaged code. 1. I know this depends on the app, but how much faster is unmanaged code vs. managed code? Is there an average figure for this? 2. Is it as...
3
by: andrew.bell.ia | last post by:
Hi, I'm trying to put together some managed and unmanaged code. Everything works fine until the program exits, and I suppose the garbage collector runs to finalize things. Then I get: ...
3
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
1
by: Bill S. | last post by:
Hi, I'm trying to create an unmanaged instance within a managed object. In the one module, I've constructed an unmanaged class that makes a lot of API calls. It compiles with no errors. In the...
0
by: MC-Advantica | last post by:
I have built a managed wrapper class and assembly that expose much functionality available in unmanaged legacy DLLs. I can write VB apps that interface with the managed assembly, and the managed...
2
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
I am trying to understand this; I have a question. I understand these statements are true: 1. Managed code is code that is executed by the CLR. 2. Managed code can access managed and unmanaged...
0
by: hanthehead | last post by:
In order to use a C# dll from our unmanaged C++ project I have created a mixed managed\unmanaged c++ dll. This dll is passed an unmanaged class which contains vector<CDBRecord> m_vecRecords member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.