13

Can anyone tell me how to hook/overlay a DirectX game in C#?

I've tried getting a fullscreen C# window to overlap a game, however it wont. After researching a little, I found out that I need to hook the game and then display the C# window.

Can anyone explain how I would do this? Would I be able to display a C# form over a DirectX game?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Joey Morani
  • 25,431
  • 32
  • 84
  • 131
  • Anyone? If anyone's seen the Steam or xFire overlay, I want it like that. The users are able to surf the net while playing games, etc. – Joey Morani Apr 16 '10 at 12:04

3 Answers3

16

You can try my example on hooking the Direct3D 9 API using C#. This utilizes EasyHook an open source .NET assembly that allows you to install hooks from managed code into unmanaged functions.

SlimDX is also used - this is an open source managed wrapper around the Direct3D libraries.

The tricky part of the hooking is determining the addresses of a COM objects' virtual functions. This is done in the above example using a small C++ helper DLL that finds the addresses from the VTable. [Update: there is a comment posted that show's how to get the function pointers from the VTable in C# also - given a IntPtr to the com object]

The example hooks the EndScene method of an IDirect3DDevice9, which is also where you would want to draw any overlays. As to displaying a form in the overlay I'm not so sure that will be so easy - I mean you can render the image easily enough, but you will have to capture inputs and manually respond/pass-thru the events to the form in question appropriately. Good luck!

Justin Stenning
  • 1,837
  • 1
  • 15
  • 19
  • 1
    Here's an updated version that has a very simple Overlay example - also 100% C#: [Direct3D 9,10 overlay](http://spazzarama.wordpress.com/2011/03/14/c-screen-capture-and-overlays-for-direct3d-9-10-and-11-using-api-hooks/) – Justin Stenning Mar 14 '11 at 05:41
  • 1
    link is down unfortunately – smedasn Nov 11 '20 at 15:50
5

(disclosure: I work for this company)

You can try our Deviare API, it has functionality to hook COM objects from C#. It's a set of COM objects that can be used from any programming language (supporting COM). An article with source code showing how to capture video and add an overlay is available: instrumenting Direct3D applications to capture video and calculate FPS

COM Spy Console and Direct Sound Capture are two examples that implements a console to hook COM objects like DirectX.

Hope it helps.

sw.
  • 3,240
  • 2
  • 33
  • 43
  • Thanks. I've applied for it. Could you explain how I could get the Deviare API to hook a directx game, and display something? Thanks again. – Joey Morani Apr 16 '10 at 14:26
  • 1
    You can ask questions in the related forums there. My recommendation is using the articles as a starting point. – sw. Apr 16 '10 at 17:46
  • 2
    That does not solve the problem for others. Please describe what the solution is. – Mathias Lykkegaard Lorenzen May 01 '13 at 20:12
  • 1
    @MathiasLykkegaardLorenzen source code for adding an overlay and capturing video using Deviare is available at: https://github.com/nektra/AVRecorderTool – sw. Jul 25 '13 at 01:54
2

Since you don't have direct DirectX Access from C# this may be tricky.

From my limited understanding of the concept:

DirectX Hooks consist of attaching to a DirectX Context and manipulating it, this may/may not require messing with another program's memory, something that's best suited to C/C++

I may be wrong though, however this is an advanced topic and my gut tells me you may have issues under .NET

Aren
  • 54,668
  • 9
  • 68
  • 101
  • How about doing the hooking with C++, and then running the C# Program from within the hook? Wouldn't that make the C# program show, or doesn't it work like that? – Joey Morani Apr 15 '10 at 23:52
  • No, you can't show a C# program within unmanaged C++ like that. One common suggestion is to use C++/CLI for a managed/unmanaged C++ wrapper half-interfacing with DirectX but also half of a "managed" library that C# can directly call. But it looks ugly. Spazzarrama (Justin S. ^)'s example of hooking and overlaying works perfectly and was an invaluable resource. – Jason Nov 13 '12 at 09:48