This post was born out of my own desire to learn about these concepts. It's always a good idea to try teaching someone else what you have just learned to help solidify it in your own head, so here you go. I wanted to learn concepts, so hopefully this post will help you learn these important concepts as well. (Will update this post based on any feedback) (Total Edit Count:6):
- [Edit2] First off: "End Scene" is the DirectX API function that the game must call when all its drawing is complete. (Clarified: A DirectX API "Present" is responsible for actually flipping the back buffer to front) Easy enough to understand. Clearly a call to this function exists in WOW, and so long as World of Warcraft is rendering in DirectX mode (DX9), it can be useful to us.
- [Edit2] "hook" or "detour" are not the same thing, but are related. Let's focus on "hook". A definition of a Function Hook (only one of many kinds of hooks, but quite common) is given on wikipedia, as well as examples of other types:
Function hooking is implemented by changing the very first few code instructions of the target function to jump to an injected [piece of] code. (Hooking - Wikipedia, the free encyclopedia)- [Edit1] A Hook can (if desired) block original code from running, however in the case of World of Warcraft, that would not be productive (with an Endscene hook)
- [Edit1] Before the hook returns to the Endscene function, it can perform any number of calls to other engine functions, or even draw more to the back buffer. This is what allows us to, for example, draw a bot's current planned movement (way-points, whatever).
- [Edit1] An EndScene hook will be run on the game's render thread, since that is where the call to EndScene originates. In World of Warcraft, the render thread is the same as the game thread.
- [Edit2] Another great thing to use is DLL Injection. Here is a great definition and four ways to inject DLLs given by Wikipedia:
Using DLL Injection, we can run code in WoW Address space which does not need to be run every frame. Such as LUA commands. When injecting the DLL, one common way is to create a new thread in the target process to call LoadLibrary (see the wikipedia page for details).DLL injection is a technique used to run code within the address space of another process by forcing it to load a dynamic-link library. (DLL injection - Wikipedia, the free encyclopedia)- [Edit3] Some people like to use a term "Code Cave" when talking about hooks or DLL injection. The "Code Cave" is a place in the target process's memory which is unused, that we can replace with our own custom code to be run. (see Xarg0's comment further down the page for an example of when a Code Cave is quite useful). Also See this source, and for an in-depth discussion of code caves on the assembly level this source
- [Edit1] VMT = Virtual Method Table. I Think it's quite likely that many programmers (I am not exempt from the following generalization, either), after learning how to Use a language, never really understand how it actually works. All that lovely inheritance, overloading, virtual functions, etc. has a lot going on "behind the scenes" so to speak. If you don't know what a VMT is, it's high time to learn: http://en.wikipedia.org/wiki/Virtual_method_table.
For making a hook to any D3D function, knowing about its Device VMT is quite necessary. If you just take a look at the D3D API, you can go through it, pick a function to hook, and grab the pointer from D3D's device VMT (You will know its index in the device VMT - same order as in d3d?.h - e.g. d3d9.h) source
Here is a post on this forum about hooking D3D11: http://www.mmowned.com/forums/world-...1-hooking.html (the "problem" comes in that there is no more "EndScene")
I am sure there are more points to add to make this clearer. Any feedback is appreciated. If this post helped you or you'd like more information on related topics, drop a line.
Useful Links / Sources:
- assembly - What is a code cave, and is there any legitimate use for one?
- The Beginners Guide to Codecaves - CodeProject
- "Dll Injection using SetWindowsHookEx() Method" Cached Page by Google (original has been lost)
- C# – Screen capture with Direct3D 9 API Hooks
- DLL injection - Wikipedia, the free encyclopedia
- Hooking - Wikipedia, the free encyclopedia
- Best Practices for Creating DLLs