http://www.codeguru.com/cpp/g-m/ ... /article.php/c11453 + F. S& ]$ ?) Y2 ?9 H% \. o
8 Z& v- r( d" [
Intercept Calls to DirectX with a Proxy DLL
5 b/ {; v- y2 j5 { E0 PShow own content on top of a full screen DirectX application $ K$ S7 A2 v6 ]7 {. @! v- v- U
Rating: none
. e3 T& r2 E0 `8 g4 E7 L& q4 M9 h. f) j& w; J$ b! n; C; W6 [
Michael Koch (view profile)
+ a# }$ w/ X& u% VMarch 13, 2006 ! o/ E, t0 g+ Q. i% [
# `4 r' m* R; z
Environment: Windows, DirectX 8/9, Visual C++ 2003 2 A9 \ E/ g! K' l" t
- ^$ x: S2 P8 L$ n
1 @5 @3 D0 U3 S
% k7 O/ O" F1 s2 k5 n
Introduction & ?. e5 q! _" [5 W! h5 i
Have you ever wanted to paint your own graphics (or text) on top of a DirectX application (for example, to show TeamSpeak information, or a self-created map, within a game)?
; T( D# r- s0 R# @
' R0 o. e) ^1 ]! k8 U6 m l+ P, z G. A6 S
(continued) + w4 V p/ k! j' u( R3 @9 [
, y! n# t. D" k0 R; J5 f- t: k3 i& b' ]0 Y ?+ m
. \! ^) a! _5 H E/ f$ _4 g" u" m3 V+ X! J: {, C# M) T* ^
MARKETPLACE
, H0 e/ i5 n1 v+ H: B% XFlowcharts from C/C++ code -- Free trial download , r0 J9 A. H) R
Understand C/C++ code in less time. Flowcharts, DataFlow, Rich Trees, Static Checking, Documentation 2 y5 A+ ]. A) L
www.sgvsarc.com 5 m& B5 \* x5 r$ [7 M L" R
New Webcast: How to Profit with Remote Support. # s n3 u/ N/ B# }; Z& V+ Q
Discover how REMOTE SUPPORT can fuel your IT business in ways you've never thought of before. 1 X# T, u7 P+ U3 J8 w6 w h
Listen to this valuable Webcast Today! www.LogMeInRescue.com
4 ~3 D7 z$ W' ~1 Z# d- o$ OAdvertiser Here / N0 o& j! l- T3 X& b
If so, the solution discussed below might help you. By using a "proxy DLL," calls to DirectX can be intercepted, data altered, and even new objects can be created and be shown within another application (read: the source code of this application does not need to be available).
' ]7 ?% e2 g: j8 h3 g6 \2 |: g
$ D% U/ M/ ?/ J5 M* ^This article's topics cover the creation of a basic proxy DLL for DirectX8/9 3D-calls (d3d8/d3d9.dll), including the full source. Based on your imagination and knowledge, you might enhance this for your very own applications and needs.
% D Q* `1 K4 W/ [
4 x- n" ]5 R" x" F$ C F- C, V1 tThe following picture shows an example application, adding graphical objects to a game. Testing application: Krakout-Arkanoid (http://www.wegroup.org/Games/Ark ... ut-RE-Arkanoid.html). This was done using a DirectX8 proxy DLL. The yellow rectangle in the upper left corner and the cube in the middle aren't part of the original game; they were created within the proxy DLL and added.
9 f! f) z. F/ Y3 X
+ U$ X' x* B$ n5 Y 4 |+ q; W- n8 I" @; u
t% ?, U; }, [
& q& R" c+ s8 O: a1 H3 e* L$ o% A% D
Concept
) p6 w0 `) k! L& @ p0 W8 q+ ~4 i j, `To obtain DirectX functionality, an application needs to load certain dynamic link libraries (DLLs); for example, d3d9.dll. A "proxy DLL" names exactly like a DLL in question and manages to get loaded by the application instead of the "real, original" one. Once loaded, the proxy loads the "real" DLL by itself and passes all calls from the application on to it. 1 }% Y+ s6 y! A
- F% T( \# L+ U' }7 VSo, a "filter" is installed between the application and its calls to regular DLL functions. Obviously, your own, new functionality now can be added when intercepting or altering certain function calls.
' s$ [8 e4 {# Q5 V. v" {; \/ J( z: c" P& z! W2 L5 L
Regular setup: Application <-> d3d9.dll (system)
! }" c0 o( s0 h! WProxy setup: Application <-> d3d9.dll(proxy) <-> d3d9.dll (system)
1 d5 \( s5 Y: ]! K8 P$ }4 T+ ~. W2 v% G, M
By the way, this concept is very similar to one used by a Microsoft DirectX programming utility, named D3DSpy. Quote from the D3DSpy help file:
, C9 ` w; C" S2 }1 ]
0 V# l- R3 Q- O2 f5 }( @3 d"D3DSpy works by providing a proxy DLL, which the application connects to and treats like the real D3D9.DLL. It intercepts all the calls and gathers various information before making the real D3D call." / Z: H6 h4 A& f: }4 q% e: A
The proxy concept doesn't work with all applications due to several reasons. It has to be tested thoroughly with an application (game) before making it available to a wider range of users.
6 k( d+ V) ~% t" J! U& g5 C$ u) u6 R2 ]: {! ]" n6 W! h- Y
There are other approaches on how to draw your own stuff in a DirectX application (like "run-time code-injection," or even altering the code of an executable file). In case the proxy DLL concept doesn't work for you, you may want to look for one of those (not covered here). % ]/ T% s/ B5 z) D( U; x" _8 L
7 Z0 n' k; j# G# Q$ k
Realisation 9 V% F2 Q7 U) z% x
I focus on Direct8/9 3D calls here. This said, you need to create a d3d8/d3d9.dll that will be called by the application instead of the "real" (system-provided) library.
. n _; K V) O$ |- I7 T. U; H& ]- W! v/ `+ }6 t2 k
Note: To test the proxy DLL, I used Krakout-Arkanoid (DX8) and Flatspace Demo (DX9), amongst others. Both are nice games, and they were on hand at that time. For debugging purposes, DebugView is a nice tool. Ah, and check out dependency walker for the examination of DLLs. 1 I" S7 k- a; z8 K1 \1 G
First, take a look at an application that uses DirectX 3D calls. It enables D3D support by loading the d3d8/d3d9.dll, and then obtains the IDirect3D8/9 interface via Direct3DCreate8/9 and creates a IDirect3DDevice8/9 with CreateDevice. 5 x5 j, L* J% [! Y" O" C
' y9 Q) w/ D5 @' x- e
If you can manage to get your d3d8/d3d9.dll loaded by the application, thus getting the call to Direct3DCreate8/9 redirected to your code, you could create a IDirect3D8/9 Interface object by yourself. The same goes for the IDirect3DDevice8/9 Interface. You then would offer all funcionality like the original interfaces to the caller's code (in fact, just by redirecting each call to a IDirect3D/IDirect3DDevice you secretly created of your own). This gives you the possibility to change data and/or add new things. + A7 d, d: \3 q$ e/ }
) \$ y" y. X+ J* ~* [ - `3 S1 t: `3 R% T
2 m3 s5 G. i4 ^! o
Any drawbacks on that? Yes.
" q9 S$ r; _# j0 c; n
* V! v% p- R6 {( `+ b1 WNormally, an application looks for a DLL within its working directory first, and then by browsing the system's path. Applications may prevent that by directly targetting the system directory (this is especially true for online games; they could consider a proxy DLL being a "hack"). And, you can't just replace the system's d3d8/d3d9.dll because you need its functions as described above.
- ^' Z3 _& ^4 S# J7 M( O# x6 U7 r3 o2 D" V
Having said this, I strongly advise to not touch the original system DLL. You might destroy your DirectX installation. Furthermore, an application might implement its own texture manager (or even use undocumented calls). Because you will not be able to handle such calls (obviously), the app possibly would not work with a proxy DLL. - b7 @5 E& j! |$ O8 G
8 W8 _+ }# m' q& A% TFinally, you know what you need:
1 b) Z$ T3 H7 H/ \4 C: {
" O! a- |9 h h* Ea file named d3d8/d3d9.dll that will be placed within the application's working directory (and hopefully get loaded by the app)
. j* \" b4 p- J; I* Ra function named "Direct3DCreate8/9" being exposed by that DLL
/ n6 S; V ~8 e3 e" T+ san interface provided by the DLL that looks exactly the same as the "real" IDirect3D interface
4 z+ G+ @' n* @$ wan interface provided by the DLL that looks exactly the same as the "real" IDirect3DDevice interface & `( }& U5 W3 I
Luckily enough, some header files that come with the DX SDK point out what calls should be handled by your "stub" Interfaces.
5 k3 }, J+ X8 i1 M8 i$ V: Q# b* ~7 u
7 F' V; a* A2 |6 p7 L- tThe Demo Project
1 W e3 f* s0 T( ?3 \; n4 UMy "basic proxy DLL" implementation consists of these items: 0 W" n. |) t2 U# Z5 _6 {
# I& X( ?7 u3 N
global routines for the DLL handling (entry point, load the system d3d8/d3d9.dll, and so on)
$ u, e# R1 |6 G) D0 ~$ g/ Han object called myIDirect3D8/9, derived from Direct3D8/9, exposing over a dozen functions $ [6 p# I0 L" H( `. |# b; W
an object called myIDirect3DDevice8/9, derived from IDirect3DDevice8/9, exposing about 100 functions
/ E0 k! d& K# H2 O; O* uBasically, all functions within the "my..." objects pass parameters on to the original system DLL, providing the possibility to change or add data. A few routines need special attention, though. 3 `) l2 N6 Y6 C1 r
& I, }* u/ A* h, c, d
::Release (both Objects)
$ ?0 B# ]+ O# u2 I9 z6 h8 AIn case the application releases the interface (and no more references are present), you destroy your own object as well.
: w# ~' s; {: h+ [( L1 b$ V
, w: ^# E% w( i% I5 w! N& `6 U7 V/ R::QueryInterface (both Objects) 8 i! L$ O# e3 Z. K3 Z3 {" L+ {
If the application queries for certain interfaces, you do of course pass the address of your very own routine, not passing the system DLL response this time.
. P4 f4 J3 a4 c. T: ^- N* h5 }6 @
myIDirect3D9::CreateDevice
$ U! L+ b* N2 N$ _1 y. KOnce this is called, you create your own D3DDevice (internally) because you need it to process the application calls. Then, you respond by sending your own object's address. % u* j6 a6 L) ~" P$ @
6 k C/ r5 p [. QFor testing purposes, the myIDirect3DDevice8/9::ShowWeAreHere function was added to the demo source code. It is called within myIDirect3DDevice8/9::Present and does nothing but create a yellow rectangle in the upper left screen. This might give you an impression of how to add you own content. 5 [. v7 B" W6 g- \1 x! d. F
, z% W) E- H4 j: PThis is a compilation from my work on http://www.proxy.mikoweb.de # B, L3 I( `0 t8 V2 S1 h( \) R1 ]6 w
rev1.1: added DX8 source, corrected typos
6 g3 H8 J4 l! H* c3 y: h6 G; ~5 \, d! S+ b, ?/ x0 m ^( k+ ]& b
About the Author
' `1 E: c H8 M9 N6 }$ Q( wI'm a hobby programmer, using VC++ .net 2003 B/ `& r* B0 O/ n2 K4 I3 W
$ z9 i% q( Q/ x- {; Y6 X
Downloads . `$ N6 v) ?* O% W/ A: o
/ p5 n( f; h/ H3 e' t
; G; B1 L( S5 X. @/ L# qproxydll_8_src.zip - Demo Project (d3d8.dll proxy, Visual C++ 2003)
& _% e; Q: E* t' _& V- t2 t6 T+ [* W# t8 @2 \5 y7 B% O/ N
proxydll_9_src.zip - Demo Project (d3d9.dll proxy, Visual C++ 2003) |