[Mono-dev] mono_fntptr_to_delegate

Bruno Lauze brunolauze at msn.com
Fri Aug 8 17:29:33 UTC 2014



I am sure someone can help me out. At one point this code did work. I
changed something and/or I just reinstalled latest trunk and it broke.

I am trying to pass C delegate to C#. Everything is working C# is calling
back the method but the parameters seems to be invalid pointers.

The C delegate was returning the delegate object, and the parameters.


I did put mono_ftnptr_to_delegate to external removing MONO_INTERNAL and
placing it under MONO_API.

One could use Marshal.GetDelegateFromPointer with mono_runtime_invoke
(Adding that way too at the bottom.)



Consider the following code:



DelegateHelper.dll: MyClass.cs:


using System;


namespace DelegateHelper


        public delegate bool TestDelegate(string arg1);


        public class MyClass


                public static bool Test(TestDelegate predicate)


                        return predicate("TEST");









#include <stdio.h>

#include <glib.h>

#include <mono/jit/jit.h>

#include <mono/metadata/object.h>

#include <mono/metadata/reflection.h>

#include <mono/metadata/assembly.h>

#include <mono/metadata/threads.h>

#include <mono/metadata/mono-config.h>


MONO_API MonoDelegate*

mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);


MonoBoolean testMethod(MonoObject *arg1, MonoObject *arg2)


        printf("Calling delegate!");

        MonoString *str = mono_object_to_string(arg2, NULL); //crash

        return TRUE;



int main (int argc, char *argv[])


        printf ("Delegate Test!\n");


        MonoDomain *domain = mono_jit_init_version("DelegateTest",


        void *__parameters__[1];

        MonoAssembly *ass = mono_assembly_open("DelegateHelper.dll", NULL);

        MonoImage *image = mono_assembly_get_image(ass);

        MonoClass *delegateClass = mono_class_from_name(image,
"DelegateHelper", "TestDelegate");


        MonoClass *testClass = mono_class_from_name(image, "DelegateHelper",


        gpointer ptr = (gpointer)testMethod;

        MonoDelegate *delegateObj = mono_ftnptr_to_delegate(delegateClass,
ptr); //Short way to call Marshal.GetDelegateFromFunctionPointer()

        MonoMethod *testMethod = mono_class_get_method_from_name(testClass,
"Test", 1);

        __parameters__[0] = delegateObj;

        MonoObject *result = mono_runtime_invoke(testMethod, NULL,
__parameters__, NULL);

        return 0;






Delegate Test!

Calling delegate!



  at <unknown> <0xffffffff>

  at (wrapper managed-to-native) object.wrapper_native_0x40ea40 ()

  at DelegateHelper.MyClass.Test (System.Func`2<string, bool>) <0x00018>

  at (wrapper runtime-invoke) <Module>.runtime_invoke_bool_object
(object,intptr,intptr,intptr) <0xffffffff>



Got a SIGSEGV while executing native code. This usually indicates

a fatal error in the mono runtime or one of the native libraries

used by your application.






This code could be used instead of mono_ftnptr_to_delegate which is normally



        MonoImage *mscorlib =
mono_assembly_get_image(mono_domain_assembly_open(domain, "mscorlib"));

        MonoClass *marshal = mono_class_from_name(mscorlib,
"System.Runtime.InteropServices", "Marshal");

        MonoMethod *getDelegate = mono_class_get_method_from_name(marshal,
"GetDelegateForFunctionPointer", 2);

        void *marshal_params[2];

        marshal_params[0] = ptr;

        marshal_params[1] = mono_type_get_object(domain,

        MonoObject *delegateObj = mono_runtime_invoke(getDelegate, NULL,
marshal_params, NULL);




Please help!!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20140808/36056571/attachment.html>

More information about the Mono-devel-list mailing list