/// <summary> /// Our present hook that will grab a copy of the backbuffer when requested. Note: this supports multi-sampling (anti-aliasing) /// </summary> /// <param name="swapChainPtr"></param> /// <param name="syncInterval"></param> /// <param name="flags"></param> /// <returns>The HRESULT of the original method</returns> int PresentHook(IntPtr swapChainPtr, int syncInterval, SharpDX.DXGI.PresentFlags flags) { this.Frame(); SwapChain swapChain = (SharpDX.DXGI.SwapChain)swapChainPtr; try { #region Draw overlay (after screenshot so we don't capture overlay as well) // Initialise Overlay Engine if (_swapChainPointer != swapChain.NativePointer || OverlayEngine == null) { if (OverlayEngine != null) { OverlayEngine.Dispose(); } OverlayEngine = new DX11.DXOverlayEngine(); OverlayEngine.Overlays.Add(new DirectXHook.Hook.Common.Overlay { Elements = { new Common.ImageElement(new Bitmap(1920, 1080)) { Location = new System.Drawing.Point(0, 0) }, //new Common.TextElement(new System.Drawing.Font("Times New Roman", 22)) { Text = "Test", Location = new System.Drawing.Point(200, 200), Color = System.Drawing.Color.Yellow, AntiAliased = false}, new Common.FramesPerSecond(new System.Drawing.Font("Arial", 16)) { Location = new System.Drawing.Point(5, 5), Color = System.Drawing.Color.Red, AntiAliased = true }, } }); OverlayEngine.Initialise(swapChain); _swapChainPointer = swapChain.NativePointer; } // Draw Overlay(s) else if (OverlayEngine != null) { foreach (var overlay in OverlayEngine.Overlays) { overlay.Frame(); } OverlayEngine.Draw(); } #endregion } catch (Exception e) { // If there is an error we do not want to crash the hooked application, so swallow the exception this.DebugMessage("PresentHook: Exeception: " + e.GetType().FullName + ": " + e.ToString()); //return unchecked((int)0x8000FFFF); //E_UNEXPECTED } // As always we need to call the original method, note that EasyHook will automatically skip the hook and call the original method // i.e. calling it here will not cause a stack overflow into this function return(DXGISwapChain_PresentHook.Original(swapChainPtr, syncInterval, flags)); }
public void ManualPresentHook(IntPtr swapChainPtr) { DebugMessage("ManualPresentHook Method start"); SwapChain swapChain = (SharpDX.DXGI.SwapChain)swapChainPtr; if (swapChainPtr != IntPtr.Zero) { try { DebugMessage("ManualPresentHook:1"); #region Draw overlay (after screenshot so we don't capture overlay as well) #region Initialise Overlay Engine if (_swapChainPointer != swapChain.NativePointer || OverlayEngine == null) { DebugMessage("ManualPresentHook:2"); NewSwapchain = true; List <IOverlayElement> oldOverlays = null; if (OverlayEngine != null) { DebugMessage("ManualPresentHook:3"); if (OverlayEngine.Overlays.Count > 0 && OverlayEngine.Overlays[0].Elements != null) { DebugMessage("ManualPresentHook:4"); oldOverlays = new List <IOverlayElement>(OverlayEngine.Overlays[0].Elements); foreach (var element in oldOverlays) { if (element is ImageElement) { DebugMessage("ManualPresentHook:5"); ((ImageElement)element).Image?.Dispose(); ((ImageElement)element).Image = null; } } } OverlayEngine.Dispose(); } DebugMessage("ManualPresentHook:6"); OverlayEngine = new DX11.DXOverlayEngine(this); OverlayEngine.Overlays = new List <IOverlay>(); OverlayEngine.Overlays.Add(new Overlay()); OverlayEngine.Overlays.Add(new Overlay()); DebugMessage("ManualPresentHook:7"); if (oldOverlays != null) { DebugMessage("ManualPresentHook:8"); OverlayEngine.Overlays[0].Elements = oldOverlays; } DebugMessage("ManualPresentHook:9"); if (ObligatoryElement != null) { DebugMessage("ManualPresentHook:10"); OverlayEngine.Overlays[0].Elements.Add(ObligatoryElement); } DebugMessage("ManualPresentHook:11"); OverlayEngine.Initialise(swapChain); DebugMessage("ManualPresentHook:12"); _swapChainPointer = swapChain.NativePointer; } #endregion // ---LOOP--- // Draw Overlay(s) if (OverlayEngine != null) { DebugMessage("ManualPresentHook:13"); foreach (var overlay in OverlayEngine.Overlays) { overlay.Frame(); } OverlayEngine.Draw(); DebugMessage("ManualPresentHook:14"); } // ---LOOP--- #endregion } catch (Exception e) { // If there is an error we do not want to crash the hooked application, so swallow the exception LogManager.DebugLog("PresentHook: Exeception: " + e.GetType().FullName + ": " + e.ToString()); LogManager.LogException(e, "PresentHook"); //return unchecked((int)0x8000FFFF); //E_UNEXPECTED } } }