[Mono-dev] storing references to mono-objects in C++ and passing them back to C#

Andreas Lagemann ae at Informatik.tu-Cottbus.DE
Fri Mar 16 09:01:53 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,
I want to do the following in a program which consists of
interacting parts of native C++(not managed) code and C# on mono
where the mono runtime is started from the C++ code and C# code
calls back to C++:

1. I want to store a reference to an already existing mono-object on
the C++ side. I currently do it this way:
    typedef MonoObject* (*getCsharpPeerFn)();

    void setCsharpPeerCallback(getCsharpPeerFn fnptr)
    {
        getCsharpPeer = fnptr;
	csharpPeer = getCsharpPeer();
	csharpPeerHandle = mono_gchandle_new(csharpPeer, TRUE);
    }
where the setCsharpPeerCallback method is called from C#-code via
P/Invoke.

That works fine so far. But additionally I would like to:

2. retrieve the stored reference from within managed code by calling
the unmanaged side. I tried something like that:

C#:	

  [DllImport("Simkernel",EntryPoint="getPeerObjectOf")]
  public static extern object getPeerObjectOf(HandleRef jarg1);

C++:
   MonoObject* getPeerObjectOf(SomeCommonClassBase* obj) {
	MyClass* myObj = dynamic_cast<MyClass*>(obj);
	return myObj ? myObj->getMyPeerObject() : NULL;
   }


When I call getPeerObjectOf from C# I get an runtime Error, stating
that marshalling of object is not implemented.

Is there another way to do what I want?
Is marshalling of type object supported in more recent mono versions
(I'm using version 1.1.13.6)?

Please help, I'm really becoming desperate about this.

Thank you for reading and best regards,

Andreas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF+pVANfV8qlW7iaERAof6AJ42HTgQqOnJSRDCaS7/U+7ZSwliqQCfZ0kd
c9orZDWbGGkhnxi8rGQ/vx8=
=Ck74
-----END PGP SIGNATURE-----



More information about the Mono-devel-list mailing list