[C#] Call functions without having to declare delegates menu

Shout-Out

User Tag List

Results 1 to 13 of 13
  1. #1
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C#] Call functions without having to declare delegates

    Below is an example of using the intermediate language generator to call functions without having to declare or register a delegate. At the current moment it only supports x86, but that is all i need it for, modify it if you want x64 support.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Runtime.InteropServices;
    
    namespace Phoenix
    {
        public static class Functions
        {
            public static retType CallFunction<retType>(IntPtr pFunction, CallingConvention CallingConvention, params Object[] args)
            {
                Type ReturnType = typeof(retType);
                Type[] ParamTypes = (from arg in args
                                     select arg.GetType()).ToArray();
    
                DynamicMethod dm = new DynamicMethod("DynamicInvoke", ReturnType, ParamTypes, typeof(Object));
                ILGenerator il = dm.GetILGenerator();
    
                for (Int32 i = 0; i < args.Length; i++)
                    il.Emit(OpCodes.Ldarg, i);
    
                il.Emit(OpCodes.Ldc_I4, pFunction.ToInt32());
                il.EmitCalli(OpCodes.Calli, CallingConvention, ReturnType, ParamTypes);
                il.Emit(OpCodes.Ret);
    
                return (retType)dm.Invoke(null, args);
            }
    
            public static void CallFunction(IntPtr pFunction, CallingConvention CallingConvention, params Object[] args)
            {
                CallFunction<Int32>(pFunction, CallingConvention, args);
            }
        }
    }
    Example Usage:
    Code:
    CallFunction((IntPtr)Offsets.LuaDoString, "DoEmote('dance')", "LuaFilename", 0)
    Last edited by bigtimt; 05-25-2010 at 09:40 AM.

    [C#] Call functions without having to declare delegates
  2. #2
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The one major drawback to doing this, is that reflection is slow (especially Reflection.Emit).

    The performance difference between the two is a fairly large difference. Not to mention; you're forced into proper type-safe functions. (And you can do proper marshaling of the function parameters and return values if you want. Not to mention, ref/out params)

    Nice work though.
    Last edited by Apoc; 05-25-2010 at 10:31 AM.

  3. #3
    Robske's Avatar Contributor
    Reputation
    305
    Join Date
    May 2007
    Posts
    1,062
    Thanks G/R
    3/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Removes the hassle of setting up a delegate if you want to test or debug something fast I suppose.

    Thanks
    "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding
    "I cried a little earlier when I had to poop" - Sku

  4. #4
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Apoc View Post
    The one major drawback to doing this, is that reflection is slow (especially Reflection.Emit).

    The performance difference between the two is a fairly large difference. Not to mention; you're forced into proper type-safe functions. (And you can do proper marshaling of the function parameters and return values if you want. Not to mention, ref/out params)

    Nice work though.
    Performance difference is only ~25x slower than a regular call, that's not that much :P

  5. #5
    XTZGZoReX's Avatar Active Member
    Reputation
    32
    Join Date
    Apr 2008
    Posts
    173
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The one major drawback to doing this, is that reflection is slow (especially Reflection.Emit).
    That's very relative.. If you cache the generated IL and call it directly instead of re-generating it all the time, you could actually achieve quite the performance gain. This is simply because the C# compiler doesn't really optimize that well (due to the strict definition of many language aspects).

  6. #6
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if you are going to cache the IL it would just be quicker to use a delegate :P

  7. #7
    XTZGZoReX's Avatar Active Member
    Reputation
    32
    Join Date
    Apr 2008
    Posts
    173
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if you are going to cache the IL it would just be quicker to use a delegate :P
    Again, depends. If we're talking performance, you can gain a lot of such by generating and caching your own IL rather than using delegates (and perhaps anonymous methods). Granted, you'll need to understand what you're doing.

  8. #8
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not talking about the speed of the code, was talking about speed of development

  9. #9
    XTZGZoReX's Avatar Active Member
    Reputation
    32
    Join Date
    Apr 2008
    Posts
    173
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not talking about the speed of the code, was talking about speed of development
    I was commenting on Apoc's post.

  10. #10
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gg (filler.)

  11. #11
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A delegate would be faster either way.

    The way I do delegates (with the Marshal.GetDelegateForFunctionPointer) literally leaves me with a delegate pointing at a func ptr.

    This Reflection.Emit code uses a mock-delegate, and DynamicInvoke, (which if you read MSDN, is stupidly slow, but required at times).

    You should do the profiling for yourself.

  12. #12
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not actually Microsoft's dynamic invoke, i just named it that because it is sort of the same thing. And I did do some profiling and delegates came out about ~25 times faster than using the above method, I just figured it was a cool piece of code :P

  13. #13
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by bigtimt View Post
    It's not actually Microsoft's dynamic invoke, i just named it that because it is sort of the same thing. And I did do some profiling and delegates came out about ~25 times faster than using the above method, I just figured it was a cool piece of code :P
    My point is proven! :P

    Your code is incredibly useful when you want to quickly toss together some stuff for messing around, and testing stuff. Definitely not something you wanna use in a to-be-released app.

Similar Threads

  1. [Bot] Injection code into wow. Do you have to call functions from the main thread?
    By Miivers in forum World of Warcraft Bots and Programs
    Replies: 2
    Last Post: 01-13-2014, 02:56 PM
  2. Get to a boss without having to kill trash mobs
    By Matt in forum World of Warcraft Exploits
    Replies: 6
    Last Post: 06-14-2006, 11:11 AM
  3. Mount in AQ without having a bugged mount
    By Matt in forum World of Warcraft Exploits
    Replies: 1
    Last Post: 03-27-2006, 06:01 PM
All times are GMT -5. The time now is 09:13 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search