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

Jonathan Chambers joncham at gmail.com
Mon Mar 19 10:56:02 EDT 2007


Andreas,

On 3/18/07, Andreas Lagemann <ae at informatik.tu-cottbus.de> wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi Jonathan,
> Thanks for the prompt answer!
> I played a little with your advice and did work it out. :)
> Jonathan Chambers wrote:
> > Hi Andreas,
> >       It sounds like what you are doing is embedding mono. Did you see
> > this page on the wiki (http://www.mono-project.com/Embedding_Mono)? To
> > answer your #2 question, I think you want your menthod to be an internal
> > call, rather than a pinvoke. Internal calls do no marshalling, so you
> > will be able to return an object directly. See the samples that ship
> > with mono, or this part of the link above (
> >
> http://www.mono-project.com/Embedding_Mono#Exposing_C_code_to_the_CIL_universe
> ).
> >
> I did read that, but I didn't consider internal calls as a solution
> - -- don't know why!!
> > So, quickly it will look something like...
> >
> > C#:
> >
> > [MethodImplAttribute(MethodImplOptions.InternalCall)]
> > public static extern object getPeerObjectOf(HandleRef jarg1);
> >
> >
> > And your C++ will almost stay the same; I'm not sure but you may need to
> > adjust your use of HandleRef since it will no longer be marshalled like
> > a pinvoke, so you'll be getting a real HandleRef structure.
> >
> It worked by passing an IntPtr instead of HandleRef and making obj
> void* in the C++ code.
>
> > Also, don't forget if you make the method an instance method rather than
> > static, the C code needs "MonoObject* this_ptr" as the first argument.
> >
> Does that mean that it is possible to use a
> pointer-to-memberfunction as internal call? I thought it had to be
> pure C function calls!


No, it has to be a C function call. I was saying you can make C#  instance
methods internal calls, as well as static methods. It's just that you need
to remember to add a MonoObject* argument as the first parameter of your C
function in that case.

Thanks again for your help,
> Andreas
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFF/ZBWNfV8qlW7iaERAjnUAJ9niR4K+OyiytMNtiU+NOmUdMt84ACfc7LX
> AQ/R1ZcSiChwphNSX6dpqSg=
> =IwNN
> -----END PGP SIGNATURE-----


Thanks,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070319/e8b8766f/attachment.html 


More information about the Mono-devel-list mailing list