Strange Issue With Multiple Hooks

May 7, 2013 at 7:15 AM
I am working on an overlay for a game and I am encountering a very strange issue. I have a hook to EndScene and a hook to DrawIndexedPrimitives. If I hook just EndScene I am able to write to the game window and put text on it. However, if I activate the DrawIndexedPrimitives hook alongside EndScene the game crashes with a NullReferenceException supposedly coming from inside my HookEndScene function. This happens even though the DrawIndexedPrimitives isn't doing much.

I have tried to fix it for the past 6 hours or so and have made no progress at all. If anyone has any suggestions that would be awesome.

Pertinent code:
//HOOK INSTALLATION
public void Run(RemoteHooking.IContext InContext, String InChannelName, IntPtr[] addresses)
{
     // install hook...

     try
     {
           EndSceneAddress = addresses[0];
           SetStreamSourceAddress = addresses[1];
           DrawIndexedPrimitivesAddress = addresses[2];
           BeginSceneAddress = addresses[3];

           EndSceneLocalHook = LocalHook.Create(EndSceneAddress, new Direct3D9Device_EndSceneDelegate(EndSceneHook), this);
                
           DrawIndexedPrimitivesLocalHook = LocalHook.Create(DrawIndexedPrimitivesAddress, new Direct3D9_DrawIndexedPrimitives(DrawIndexedPrimitivesHook), this);
                
           DrawIndexedPrimitivesLocalHook.ThreadACL.SetExclusiveACL(new Int32[1]);
           EndSceneLocalHook.ThreadACL.SetExclusiveACL(new Int32[1]);
     }
     catch (Exception ExtInfo)
     {
            Interface.ReportException(ExtInfo);
            return;
     }
}
//DRAWINDEXEDPRIMITIVES basically empty hook
int DrawIndexedPrimitivesHook(IntPtr devicePtr, SlimDX.Direct3D9.PrimitiveType primitiveType,
                                        int baseVertexIndex, int minimumVertexIndex,
                                        int numVertices, int startIndex, int primCount)
{
     using (SlimDX.Direct3D9.Device device = SlimDX.Direct3D9.Device.FromPointer(devicePtr))
     {
         try
         {
             return device.DrawIndexedPrimitives(primitiveType, baseVertexIndex, minimumVertexIndex, numVertices, startIndex, primCount).Code;
         }
         catch(Exception e)
         {
               Interface.ReportException(e);
               return device.DrawIndexedPrimitives(primitiveType, baseVertexIndex, minimumVertexIndex, numVertices, startIndex, primCount).Code;
         }
     }
}
int EndSceneHook(IntPtr devicePtr)
{
    using (SlimDX.Direct3D9.Device device = SlimDX.Direct3D9.Device.FromPointer(devicePtr))
    {
     // If you need to capture at a particular frame rate, add logic here decide whether or not to skip the frame
        try
        {
            SlimDX.Color4 MenuLineColor = new SlimDX.Color4(Color.Red);
            SlimDX.Color4 FontColor = new SlimDX.Color4(Color.Purple);
            SlimDX.Direct3D9.Font MenuFont = new SlimDX.Direct3D9.Font(device, 15, 0, SlimDX.Direct3D9.FontWeight.Bold, 1, false, SlimDX.Direct3D9.CharacterSet.Default, SlimDX.Direct3D9.Precision.Default, SlimDX.Direct3D9.FontQuality.Antialiased,                     SlimDX.Direct3D9.PitchAndFamily.DontCare,"Verdana");
                    
             SlimDX.Direct3D9.Line MenuLine = new SlimDX.Direct3D9.Line(device);
                    
             if (MenuFont != null && MenuLine != null && FontColor != null)
             {
                 Rectangle fontPos = new Rectangle();
                 fontPos.X = 10;
                 fontPos.Y = 10;
                 fontPos.Width = 10 + 120;
                 fontPos.Height = 15 + 16;
                 string text = "Helllo world";

                 if (MenuFont != null)
                 {
                     MenuFont.DrawString(null, text, fontPos, SlimDX.Direct3D9.DrawTextFormat.NoClip, FontColor);
                  }
                  return device.EndScene().Code;    
           }
           catch(Exception e)
           {
                Interface.ReportException(e);
                return device.EndScene().Code;
            }
       return device.EndScene().Code;
       }
}