Connecting Tech Pros Worldwide Forums | Help | Site Map

Mixed managed and unmanaged code - How to do it?

Thorsten
Guest
 
Posts: n/a
#1: Nov 17 '05
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 :



Marcus Heege
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Mixed managed and unmanaged code - How to do it?


> 1) I have two classes defined in two header files, it is possible to have[color=blue]
> 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.[/color]

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>
[color=blue]
> 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*>?[/color]

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


Thorsten
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Mixed managed and unmanaged code - How to do it?


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" <NOSPAM@heege.net> wrote in message
news:OI7GajoxFHA.4032@TK2MSFTNGP15.phx.gbl...[color=blue][color=green]
>> 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.[/color]
>
> 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>
>[color=green]
>> 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*>?[/color]
>
> 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
>
>[/color]


Marcus Heege
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Mixed managed and unmanaged code - How to do it?


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" <TK777@gmx.de> wrote in message
news:uGBAz5oxFHA.3856@tk2msftngp13.phx.gbl...[color=blue]
> 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" <NOSPAM@heege.net> wrote in message
> news:OI7GajoxFHA.4032@TK2MSFTNGP15.phx.gbl...[color=green][color=darkred]
>>> 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.[/color]
>>
>> 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>
>>[color=darkred]
>>> 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*>?[/color]
>>
>> 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
>>
>>[/color]
>
>[/color]


Closed Thread


Similar .NET Framework bytes