Technical questions about in-process managed code. menu

Shout-Out

User Tag List

Results 1 to 2 of 2
  1. #1
    galpha's Avatar Member
    Reputation
    5
    Join Date
    Nov 2007
    Posts
    48
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Technical questions about in-process managed code.

    Hi,

    I've been coding a Wow framework for a good amount of time and I'm starting to think about refactoring my code since it's starting to be a maintenance hell. The biggest problem I have so far is in relation to statics. Basically, I'm using WhiteMagic by Apoc (awsome lib!!!), but so far I have a big drawback with it. In all of my code that uses hook, I have to use static delegates/classes.

    Now, from what I understand, we have to do that to keep an instance of our delegate alive if we don't want it to be thrown away by the garbage collector. I've looked around on forums and blogs about what could be the solutions, and I've found the GCHandle class. The following method seems to be interesting :

    public static GCHandle Alloc(Object value, GCHandleType type);

    Basically, what I thought was to use this method to pin my delegates in instance classes (non-static). Let's take for e.g. an EndScene hook (doesn't include 100% of the code, only important parts).:

    Code:
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
            public delegate int EndSceneDelegate(IntPtr instance);
    
            private void SetupDirectxHook()
            {
                EndSceneDelegate EndSceneHandl = new EndSceneDelegate(EndSceneDetour);
    
                // Call Free() later when necessary (most then likely on framework closing).
                GCHandle.Alloc(EndSceneHandl, GCHandleType.Pinned);
    
                IntPtr endScenePtr = GetDevicePointer((UInt32)Direct3DAPI.EndSceneOffset);
                EndSceneDelegate EndSceneTypedef = Magic.Instance.RegisterDelegate<EndSceneDelegate>(endScenePtr);
    
                Magic.Instance.Detours.CreateAndApply(EndSceneTypedef, EndSceneHandl, "EndScene");
            }
    
            private int EndSceneDetour(IntPtr instance)
            {
                return (int)Magic.Instance.Detours["EndScene"].CallOriginal(instance);
            }
    Now, this would get me rid of any statics. Now *IF* this works, the problem would be with certain type of parameters. GCHandle.Alloc throws a ArgumentException on the following case (quote from MSDN) : An instance with nonprimitive (non-blittable) members cannot be pinned.

    Anyway, enought with my wall of text, are there cases where this wouldn't work, or why it wouldn't work?
    Last edited by galpha; 02-26-2011 at 03:50 PM.

    Technical questions about in-process managed code.
  2. #2
    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)
    Why don't you just store the delegate in a private instance field?

    EDIT: Just for the record, we discussed this over on IRC.
    Last edited by XTZGZoReX; 02-27-2011 at 07:02 AM.

Similar Threads

  1. [Core] Question about the 1.12.1 connection process
    By bigtimt in forum WoW EMU Questions & Requests
    Replies: 0
    Last Post: 07-24-2012, 10:03 PM
  2. Question about this piece of code " IsControlKeyDown()"
    By Eliteplague in forum WoW Bots Questions & Requests
    Replies: 2
    Last Post: 04-12-2011, 09:53 PM
  3. Question about process hash & Warden.
    By Sel3n in forum WoW Memory Editing
    Replies: 15
    Last Post: 07-29-2009, 12:41 AM
  4. another CE question, about process guard
    By mwo in forum World of Warcraft General
    Replies: 0
    Last Post: 01-07-2007, 11:18 PM
  5. questions about model editing
    By Avianar47 in forum World of Warcraft General
    Replies: 2
    Last Post: 07-08-2006, 09:41 PM
All times are GMT -5. The time now is 04:56 AM. 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