[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Scavanger/MemoryModule.net/master/MemoryModule.net/Program.cs [Back]  [Original]

using System;
using System.Runtime.InteropServices;
using System.IO;

namespace Scavanger.MemoryModule
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        struct Foo
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
            public int[] bar;
        }

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate int QuxDelegate(IntPtr strPtr);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate int AddNumbersDelegate(int a, int b);

        [UnmanagedFunctionPointer(CallingConvention.Winapi)]
        private delegate int TlsDelegate();

        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("AddNumbers");
                        Console.WriteLine("The Answer: {0:G}", AddNumbers2(38, 4));
                        
                        // Working with structs
                        QuxDelegate qux = memModule.GetDelegateFromFuncName("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);
        }
    }
}
    

Web Proxy Viewer  |  New URL  |  Original Page