Mixed managed and unmanaged code - How to do it?

T

Thorsten

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 :
 
M

Marcus Heege

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();
};
}
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/micr...e0a87a1a61b/1da4e633322dda9d#1da4e633322dda9d
[2]
http://groups.google.com/group/micr...7ece6222939/a2439de50486887f#a2439de50486887f
 
T

Thorsten

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 said:
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();
};
}
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/micr...e0a87a1a61b/1da4e633322dda9d#1da4e633322dda9d
[2]
http://groups.google.com/group/micr...7ece6222939/a2439de50486887f#a2439de50486887f
 
M

Marcus Heege

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 said:
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 said:
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();
};
}
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/micr...e0a87a1a61b/1da4e633322dda9d#1da4e633322dda9d
[2]
http://groups.google.com/group/micr...7ece6222939/a2439de50486887f#a2439de50486887f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top