public /*unsafe */ void RunAsm(params byte[] asm)
            {
                IntPtr buf = VirtualAlloc(IntPtr.Zero, (uint)asm.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

                Marshal.Copy(asm, 0, buf, asm.Length);

                IntReturner ptr = (IntReturner)Marshal.GetDelegateForFunctionPointer(buf, typeof(IntReturner));

                //fixed (byte* newBuffer = &asm[0])
                //{
                ptr();
                //del(newBuffer);
                //}
                //Console.WriteLine(ptr());
            }
    static void Main(string[] args)
    {
        List <byte> bodyBuilder = new List <byte>();

        bodyBuilder.Add(0xb8);                           // MOV EAX,
        bodyBuilder.AddRange(BitConverter.GetBytes(42)); // 42
        bodyBuilder.Add(0xc3);                           // RET
        byte[] body = bodyBuilder.ToArray();
        IntPtr buf  = VirtualAlloc(IntPtr.Zero, (uint)body.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

        Marshal.Copy(body, 0, buf, body.Length);
        IntReturner ptr = (IntReturner)Marshal.GetDelegateForFunctionPointer(buf, typeof(IntReturner));

        Console.WriteLine(ptr());
    }
Example #3
0
        public static void test()
        {
            List <byte> bodyBuilder = new List <byte>();

            bodyBuilder.Add(0xb8);
            bodyBuilder.AddRange(BitConverter.GetBytes(42));
            bodyBuilder.Add(0xc3);
            byte[] body = bodyBuilder.ToArray();
            IntPtr buf  = VirtualAlloc(IntPtr.Zero, (uint)body.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

            Marshal.Copy(body, 0, buf, body.Length);

            IntReturner ptr = (IntReturner)Marshal.GetDelegateForFunctionPointer(buf, typeof(IntReturner));

            Console.WriteLine(ptr());

            int here = 5;

            int n = addInt(5, 7);
        }
Example #4
0
        static void Main(string[] args)
        {
            WorkHandler wh = DoSomething;

            Thread.CurrentThread.Name = "Main Thread";
            var ar = wh.BeginInvoke(20, asyncResult => { Console.WriteLine("DoSomething Done"); }, null);

            for (int i = 0; i < 20; i++)
            {
                IntReturner ir  = GetInteger;
                var         ar2 = ir.BeginInvoke(asyncResult =>
                {
                    Console.WriteLine("GetInteger Done");
                }, null);
            }

            int remainingWorkerThreads, remainingIOThreads;

            ThreadPool.GetAvailableThreads(out remainingWorkerThreads, out remainingIOThreads);
            Console.WriteLine($"While the worker thread is doing its work, here we are in the {Thread.CurrentThread.Name} again. Remaining threads: {remainingWorkerThreads}");

            Console.ReadKey();
        }