Пример #1
0
        private static void TestDelegate()
        {
            Console.WriteLine("Testing Delegate");
            Delegate_Int del = new Delegate_Int(Sum);

            ThrowIfNotEquals(true, ReversePInvoke_Int(del), "Delegate marshalling failed.");
            unsafe
            {
                //
                // We haven't instantiated Delegate_Unused and nobody
                // allocates it. If a EEType is not constructed for Delegate_Unused
                // it will fail during linking.
                //
                ReversePInvoke_Unused(null);
            }

            Delegate_Int closed = new Delegate_Int((new ClosedDelegateCLass()).Sum);

            ThrowIfNotEquals(true, ReversePInvoke_Int(closed), "Closed Delegate marshalling failed.");

            Delegate_String ret = GetDelegate();

            ThrowIfNotEquals(true, ret("Hello World!"), "Delegate as P/Invoke return failed");

            Delegate_String d = new Delegate_String(new ClosedDelegateCLass().GetString);

            ThrowIfNotEquals(true, Callback(ref d), "Delegate IN marshalling failed");
            ThrowIfNotEquals(true, d("Hello World!"), "Delegate OUT marshalling failed");

            Delegate_String ds = new Delegate_String((new ClosedDelegateCLass()).GetString);

            ThrowIfNotEquals(true, ReversePInvoke_String(ds), "Delegate marshalling failed.");
        }
Пример #2
0
        private static void TestDelegate()
        {
            Console.WriteLine("Testing Delegate");

            Delegate_Int del = new Delegate_Int(Sum);

            ThrowIfNotEquals(true, ReversePInvoke_Int(del), "Delegate marshalling failed.");

            Delegate_Int_AggressiveInlining del_aggressive = new Delegate_Int_AggressiveInlining(Sum);

            ThrowIfNotEquals(true, ReversePInvoke_Int_AggressiveInlining(del_aggressive), "Delegate marshalling with aggressive inlining failed.");

            unsafe
            {
                //
                // We haven't instantiated Delegate_Unused and nobody
                // allocates it. If a EEType is not constructed for Delegate_Unused
                // it will fail during linking.
                //
                ReversePInvoke_Unused(null);
            }

            Delegate_Int closed = new Delegate_Int((new ClosedDelegateCLass()).Sum);

            ThrowIfNotEquals(true, ReversePInvoke_Int(closed), "Closed Delegate marshalling failed.");

            Delegate_String ret = GetDelegate();

            ThrowIfNotEquals(true, ret("Hello World!"), "Delegate as P/Invoke return failed");

            Delegate_String d = new Delegate_String(new ClosedDelegateCLass().GetString);

            ThrowIfNotEquals(true, Callback(ref d), "Delegate IN marshalling failed");
            ThrowIfNotEquals(true, d("Hello World!"), "Delegate OUT marshalling failed");

            Delegate_String ds = new Delegate_String((new ClosedDelegateCLass()).GetString);

            ThrowIfNotEquals(true, ReversePInvoke_String(ds), "Delegate marshalling failed.");

            Delegate_OutString dos = new Delegate_OutString((out string x) =>
            {
                x = "Hello there!";
                return(true);
            });

            ThrowIfNotEquals(true, ReversePInvoke_OutString(dos), "Delegate string out marshalling failed.");

            Delegate_Array da = new Delegate_Array((new ClosedDelegateCLass()).CheckArray);

            ThrowIfNotEquals(true, ReversePInvoke_Array(da), "Delegate array marshalling failed.");

            IntPtr procAddress = GetFunctionPointer();
            SetLastErrorFuncDelegate funcDelegate =
                Marshal.GetDelegateForFunctionPointer <SetLastErrorFuncDelegate>(procAddress);

            funcDelegate(0x204);
            ThrowIfNotEquals(0x204, Marshal.GetLastWin32Error(), "Not match");
        }
Пример #3
0
 static extern bool Callback(ref Delegate_String d);
Пример #4
0
 static extern bool ReversePInvoke_String(Delegate_String del);