Beispiel #1
0
        static void Main(string[] args)
        {
#if DEBUG
#if WIN64
            string dllPath = @"..\..\..\..\x64\Debug\SampleDll.dll";
#elif WIN32
            string dllPath = @"..\..\..\Debug\SampleDll.dll";
#endif
#else
#if WIN64
            string dllPath = @"..\..\..\..\X64\Release\SampleDll.dll";
#elif WIN32
            string dllPath = @"..\..\..\Release\SampleDll.dll";
#endif
#endif
            if (File.Exists(dllPath))
            {
                try
                {
                    using (MemoryModule memModule = new MemoryModule(File.ReadAllBytes(dllPath)))
                    {
                        // Normal function call
                        AddNumbersDelegate AddNumbers = (AddNumbersDelegate)memModule.GetDelegateFromFuncName("AddNumbers", typeof(AddNumbersDelegate));
                        Console.WriteLine("The Answer: {0:G}", AddNumbers(40, 2));

                        // Normal function call, with generics
                        AddNumbersDelegate AddNumbers2 = memModule.GetDelegateFromFuncName <AddNumbersDelegate>("AddNumbers");
                        Console.WriteLine("The Answer: {0:G}", AddNumbers2(38, 4));

                        // Working with structs
                        QuxDelegate qux = memModule.GetDelegateFromFuncName <QuxDelegate>("Qux");
                        Foo         foo = new Foo
                        {
                            bar = new int[] { 23, 5, 42 }
                        };

                        IntPtr fooPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Foo)));
                        Marshal.StructureToPtr(foo, fooPtr, true);
                        Console.WriteLine("Still the answer: {0:D}", qux(fooPtr));

                        Marshal.FreeHGlobal(fooPtr);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
            else
            {
                Console.WriteLine("Error: Dll not found!");
            }

            Console.ReadKey(true);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
#if DEBUG
            string dllPath = @"..\..\..\Debug\SampleDll.dll";
#else
            string dllPath = @"..\..\..\Release\SampleDll.dll";
#endif

            if (File.Exists(dllPath))
            {
                try
                {
                    using (MemoryModule memModule = new MemoryModule(File.ReadAllBytes(dllPath)))
                    {
                        AddNumbersDelegate AddNumbers = (AddNumbersDelegate)memModule.GetDelegateFromFuncName("AddNumbers", typeof(AddNumbersDelegate));
                        if (AddNumbers != null)
                        {
                            Console.WriteLine("The Answer: {0:G}", AddNumbers(40, 2));
                        }

                        QuxDelegate qux = (QuxDelegate)memModule.GetDelegateFromFuncName("Qux", typeof(QuxDelegate));

                        Foo foo = new Foo();
                        foo.bar = new int[] { 23, 5, 42 };

                        IntPtr fooPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Foo)));
                        Marshal.StructureToPtr(foo, fooPtr, true);

                        if (qux != null)
                        {
                            Console.WriteLine("Still the answer: {0:D}", qux(fooPtr));
                        }

                        Marshal.FreeHGlobal(fooPtr);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
            else
            {
                Console.WriteLine("Error: Dll not found!");
            }

            Console.ReadKey(true);
        }
        public static void Main(string[] args)
        {
            AddNumbersDelegate del = new AddNumbersDelegate(AddNumbers);

            int firstNumber  = int.Parse(Console.ReadLine());
            int secondNumber = int.Parse(Console.ReadLine());

            int result = del(firstNumber, secondNumber);

            Console.WriteLine("Addition = {0}", result);

            del = MultiplyNumbers;

            result = del(firstNumber, secondNumber);

            Console.WriteLine("Multiplication = {0}", result);
        }