Problem applying end scene hook menu

User Tag List

Results 1 to 7 of 7
  1. #1
    wag321's Avatar Member
    Reputation
    1
    Join Date
    Jul 2010
    Posts
    17
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem applying end scene hook

    I've written an endscene hook, got the basic idea from whitemagic (thanks apoc)
    I'm having some problems with it though, first I have an issue calling the original endscene when my endscene tries to it crashes the game, and secondly I can only get my endscene ran if I step through my code in the debugger, if I just run it it doesn't even run the first time but crashes the game in another manner. Anyway code below, I guess I'm doing something stupid but I've been staring at it for a while now

    Code:
    public class Hook
        {
            [UnmanagedFunctionPointer(CallingConvention.Winapi)]
            private delegate IntPtr EndSceneDelegate(IntPtr instance);
            private static readonly EndSceneDelegate DEL_Original = EndScene;
            private static readonly EndSceneDelegate DEL_New = (EndSceneDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)Interaction.DMA.GeneralFunctions.GetEndscene(), typeof(EndSceneDelegate));
            private static readonly uint PTR_Original = (uint)Marshal.GetFunctionPointerForDelegate(DEL_New);
            private static readonly uint PTR_New = (uint)Marshal.GetFunctionPointerForDelegate(DEL_Original);
    
            private static readonly byte[] OriginalBytes = Interaction.DMA.RAW.ReadBytes((uint)PTR_Original, 6);
            private static readonly byte[] BTS_NewAddress = BitConverter.GetBytes(PTR_New);
            private static object MEMLOCK = new object();
            private static List<byte> ReplacedBytes = new List<byte>();
    
            private static IntPtr EndScene(IntPtr instance)
            {
                lock (MEMLOCK)
                {
                    Remove();
                    DEL_New.DynamicInvoke(instance);
                    Apply();
                    return instance;
                }
            }        
            public static void create()
            {
                ReplacedBytes.Add(0x68);
                ReplacedBytes.AddRange(BTS_NewAddress);
                ReplacedBytes.Add(0xC3);
                Apply();
            }
            private static void Apply()
            {
                lock (MEMLOCK)
                {
                    Interaction.DMA.RAW.WriteBytes(PTR_Original, ReplacedBytes.ToArray());
                }
            }
            private static void Remove()
            {
                lock (MEMLOCK)
                {
                    Interaction.DMA.RAW.WriteBytes(PTR_Original, OriginalBytes);
                }
            }
        }

    Problem applying end scene hook
  2. #2
    adaephon's Avatar Active Member
    Reputation
    76
    Join Date
    May 2009
    Posts
    167
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Currently you're getting a managed delegate for the EndScene function pointer
    Code:
    DEL_New = (EndSceneDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)Interaction.DMA.GeneralFunctions.GetEndscene(), typeof(EndSceneDelegate));
    and then getting your unmanaged pointer to that function by recalling a Marshal method.
    Code:
    PTR_Original = (uint)Marshal.GetFunctionPointerForDelegate(DEL_New);
    Firstly, you already have your unmanaged pointer for the original function (it's the pointer you passed to Marshal.GetDelegateForFunctionPointer in the line above. Secondly, doing it doubly like this I'm fairly sure is not supported and will give inaccurate results (read the remarks here: Marshal.GetDelegateForFunctionPointer Method (System.Runtime.InteropServices)).

    On another note, your naming is kind of all over the place, making the code a bit more obscure. Some advice would be to stick with a common naming convention (why are some variables all caps (MEMLOCK), some just caps_ prefixed (PTR_New) and some just standard (ReplacedBytes)?) Also, your DEL_Original points to your custom EndScene method (the new one), not the original (that's DEL_New), but then your PTR_New points to your new method, not the original, and PTR_Original points to the original. At the very least they should be consistent, and ideally they should be named to accurately reflect what they actually are.

  3. #3
    streppel's Avatar Active Member
    Reputation
    78
    Join Date
    Mar 2007
    Posts
    196
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might be wrong about this, but as you are passing the first parameter called instance as intptr I guess that the function is a ThisCall function. You make it be a winapi.And seriously take a look at your naming

  4. #4
    adaephon's Avatar Active Member
    Reputation
    76
    Join Date
    May 2009
    Posts
    167
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Normally it would be ThisCall but Windows specifically declares these methods as winapi which is just a typedef for stdcall I think
    Last edited by adaephon; 07-10-2011 at 03:49 AM. Reason: Auto correct

  5. #5
    wag321's Avatar Member
    Reputation
    1
    Join Date
    Jul 2010
    Posts
    17
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, I've cleaned up the naming and the pointers and delegates being opposites was accidental so that's fixed.
    I've played around with it a bit and I'm now having a problem calling the original - it doesn't return and wow crashes.
    My endscene looks like
    Code:
            private static IntPtr EndScene(IntPtr instance)
            {
                Remove();
                System.Windows.Forms.MessageBox.Show("Hiya");
                IntPtr res = (IntPtr)delOriginal.DynamicInvoke(instance);
                //Apply();
                return res;
            }
    So I thought that there's a problem with the way I'm removing the hook, so then when I create it I added a remove straight after apply which works fine my Endscene never get's called as you would expect. Or else my delOriginal is bad
    Code:
    delOriginal = (EndSceneDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)ptrOriginal, typeof(EndSceneDelegate));
    But I know that ptrOriginal is good, Am I missing something here?

  6. #6
    _Mike's Avatar Contributor
    Reputation
    310
    Join Date
    Apr 2008
    Posts
    531
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by streppel View Post
    I might be wrong about this, but as you are passing the first parameter called instance as intptr I guess that the function is a ThisCall function. You make it be a winapi.And seriously take a look at your naming
    DirectX object methods are stdcall because COM interfaces are supposed to be language neutral. If they had used thiscall instead it would have limited the number of languages that could use them.
    Edit:
    Have a look at the interface declarations in the d3d9.h header file if you want more details. If you use D3D from C it emulates OOP by creating a "vtable" that is a struct of function pointers.
    Last edited by _Mike; 07-10-2011 at 08:21 AM. Reason: spelling

  7. #7
    wag321's Avatar Member
    Reputation
    1
    Join Date
    Jul 2010
    Posts
    17
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit. Nvm got it
    Last edited by wag321; 07-11-2011 at 01:35 PM.

Similar Threads

  1. Problem with DirectX CreateDevice Hooking
    By rik.chong in forum WoW Memory Editing
    Replies: 4
    Last Post: 11-30-2012, 04:50 AM
  2. WndProc (Mouse Input Hook) Problem
    By Amrok in forum WoW Memory Editing
    Replies: 10
    Last Post: 10-30-2011, 07:30 AM
  3. D3D Hook problem
    By Master674 in forum WoW Memory Editing
    Replies: 5
    Last Post: 09-20-2011, 12:04 PM
  4. Problems with my proxy. At wits end. Need advice ...
    By adapa in forum WoW Bots Questions & Requests
    Replies: 0
    Last Post: 07-18-2011, 04:15 PM
  5. Halls of Origination end quest scene glitch
    By Blazeflack in forum World of Warcraft Exploration
    Replies: 2
    Last Post: 12-31-2010, 01:49 AM
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