예제 #1
0
    public static void RunGetFcnPtrSingleMulticastTest()
    {
        Console.WriteLine($"Running {nameof(RunGetFcnPtrSingleMulticastTest)}...");

        {
            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate <DelegateWithLong>(s_DelWithLongBool);
            Assert.IsTrue(FunctionPointerNative.CheckFcnPtr(fcnptr));
        }

        {
            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate <MultiDelegateWithLong>(s_MultidelWithLong);
            FunctionPointerNative.CheckFcnPtr(fcnptr);
        }
    }
예제 #2
0
    public static void RunGetDelForFcnPtrTest()
    {
        Console.WriteLine($"Running {nameof(RunGetDelForFcnPtrTest)}...");

        // Delegate -> FcnPtr -> Delegate
        {
            VoidDelegate md     = new VoidDelegate(VoidVoidMethod);
            IntPtr       fcnptr = Marshal.GetFunctionPointerForDelegate <VoidDelegate>(md);

            VoidDelegate del = (VoidDelegate)Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(VoidDelegate));
            Assert.AreEqual(md.Target, del.Target, "Failure - the Target of the funcptr->delegate should be equal to the original method.");
            Assert.AreEqual(md.Method, del.Method, "Failure - The Method of the funcptr->delegate should be equal to the MethodInfo of the original method.");
        }

        // Native FcnPtr -> Delegate
        {
            IntPtr       fcnptr = FunctionPointerNative.GetVoidVoidFcnPtr();
            VoidDelegate del    = (VoidDelegate)Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(VoidDelegate));
            Assert.AreEqual(null, del.Target, "Failure - the Target of the funcptr->delegate should be null since we provided native funcptr.");
            Assert.AreEqual("Invoke", del.Method.Name, "Failure - The Method of the native funcptr->delegate should be the Invoke method.");

            // Round trip of a native function pointer is never legal for a non-concrete Delegate type
            Assert.Throws <ArgumentException>(() =>
            {
                Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(Delegate));
            });

            Assert.Throws <ArgumentException>(() =>
            {
                Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(MulticastDelegate));
            });
        }

        void VoidVoidMethod()
        {
            Console.WriteLine("Simple method to get a delegate for");
        }
    }
예제 #3
0
    public static void RunGetDelForFcnPtrTest()
    {
        Console.WriteLine($"Running {nameof(RunGetDelForFcnPtrTest)}...");

        // Delegate -> FcnPtr -> Delegate
        {
            VoidDelegate md     = new VoidDelegate(VoidVoidMethod);
            IntPtr       fcnptr = Marshal.GetFunctionPointerForDelegate <VoidDelegate>(md);

            VoidDelegate del = (VoidDelegate)Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(VoidDelegate));
            Assert.Equal(md.Target, del.Target);
            Assert.Equal(md.Method, del.Method);
        }

        // Native FcnPtr -> Delegate
        {
            IntPtr       fcnptr = FunctionPointerNative.GetVoidVoidFcnPtr();
            VoidDelegate del    = (VoidDelegate)Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(VoidDelegate));
            Assert.Equal(null, del.Target);
            Assert.Equal("Invoke", del.Method.Name);

            // Round trip of a native function pointer is never legal for a non-concrete Delegate type
            Assert.Throws <ArgumentException>(() =>
            {
                Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(Delegate));
            });

            Assert.Throws <ArgumentException>(() =>
            {
                Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(MulticastDelegate));
            });
        }

        void VoidVoidMethod()
        {
            Console.WriteLine("Simple method to get a delegate for");
        }
    }
예제 #4
0
    public delegate void MultiDelegateWithLong(long l); //Multicast delegate

    public static void RunGetFcnPtrSingleMulticastTest()
    {
        Console.WriteLine($"Running {nameof(RunGetFcnPtrSingleMulticastTest)}...");

        DelegateWithLong      del      = new DelegateWithLong(Method);
        MultiDelegateWithLong multidel = new MultiDelegateWithLong(Method2);

        {
            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate <DelegateWithLong>(del);
            Assert.IsTrue(FunctionPointerNative.CheckFcnPtr(fcnptr));
        }

        {
            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate <MultiDelegateWithLong>(multidel);
            FunctionPointerNative.CheckFcnPtr(fcnptr);
        }

        bool Method(long l)
        {
            if (l != 999999999999)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }

        void Method2(long l)
        {
            if (l != 999999999999)
            {
                throw new Exception("Failed multicast call");
            }
        }
    }