mardi 30 avril 2019

How to call a function from a delegate and a function pointer inside

I have the code below, calling a function with delegate, and a function inside the delegate. But the system throw a exception of " Cannot marshal 'return value': Invalid managed/unmanaged type combination." in Marshal.GetDelegateForFunctionPointer, could anyone help on how tosolve this?

namespace hello
{ 
    class test
    { 
        public delegate IPAddress[] DnsFuncDelegate(string message);
        public DnsFuncDelegate dns = null;

        static void Main(string[] args)
        {
            BindingFlags anyType = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            string filePath1 = "I:\\psnet\\System.dll";
            Assembly systemAssembly1 = Assembly.LoadFile(filePath1);
            Type targetMethodClass1 = systemAssembly1.GetType("System.Net.Dns");
            var targetMethodType = new Type[] { typeof(String), };
            MethodInfo target1 = targetMethodClass1.GetMethod("GetHostAddresses", anyType, null, targetMethodType, null);
            RuntimeHelpers.PrepareMethod(target1.MethodHandle);
            IntPtr target1Addr = target1.MethodHandle.GetFunctionPointer();

            DnsFuncDelegate dns = (DnsFuncDelegate)Marshal.GetDelegateForFunctionPointer(target1Addr, typeof(DnsFuncDelegate));

            IPAddress[] addresses2 = dns("localhost");
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire