设为首页收藏本站官方微博

【汉化资料】在DirectX中使用代理DLL技术

[复制链接]
查看: 3475|回复: 0
打印 上一主题 下一主题

【汉化资料】在DirectX中使用代理DLL技术

跳转到指定楼层
楼主
发表于 2009-3-20 07:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

【汉化资料】在DirectX中使用代理DLL技术

http://www.codeguru.com/cpp/g-m/ ... /article.php/c11453 + C/ }% T) q5 L9 X
+ |6 q  z& e/ ]% k- a; N+ S  J% N
Intercept Calls to DirectX with a Proxy DLL " c) |7 P8 C& G1 C, i2 |+ |
Show own content on top of a full screen DirectX application
* i5 ?4 l6 ]. ^# M5 T( t0 sRating: none + r" R8 \% m! K1 {' D$ }2 _+ L3 j
2 v3 n: ~8 q1 |& F2 d) ~
Michael Koch (view profile)
1 k4 J/ u, A  G, UMarch 13, 2006  : }6 {. [$ o: F
. ]4 K& L. t6 e
Environment:  Windows, DirectX 8/9, Visual C++ 2003 0 {& G$ i3 i! w0 s( m

; p/ T- E0 q& \/ T. F, u9 d- Y3 t* T- u6 C7 V! h5 r

- c$ y& E! T2 I4 pIntroduction
: Z8 S7 [! m! m6 w% }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)? & m' m& }4 g9 D5 [, Y: [4 T# C

' @* w% m, U) C* v9 z- J
3 B  a7 e& h( V; m(continued)
' `. r( U. n) F8 U
6 D; P& l7 i: C1 w  d( W: f/ W1 C: H- R9 b' \

- n% G  Q0 n  C6 @; A7 {7 ^* o- \$ n. \1 z- _  |
MARKETPLACE 2 u: g) a2 c3 q1 g/ Q% a2 |; E
Flowcharts from C/C++ code -- Free trial download - p( b' z5 {2 V' y# f
Understand C/C++ code in less time. Flowcharts, DataFlow, Rich Trees, Static Checking, Documentation
/ l" ?# l; a' d) ?www.sgvsarc.com
# e. G$ t$ s4 }" PNew Webcast: How to Profit with Remote Support. 9 g8 n" R: f& q8 `
Discover how REMOTE SUPPORT can fuel your IT business in ways you've never thought of before.
" ]) t7 g* {9 `* Q: z1 Z; [Listen to this valuable Webcast Today! www.LogMeInRescue.com " `. `) U* y2 P* |% G
Advertiser Here  
9 \( G; t! O0 {! @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).
) x% D+ p# z& ]# Q( G/ O' n
+ L+ A, _7 Y( i: {+ d3 i7 T) EThis 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.
0 O/ s* D; y7 b0 f( B1 Q# q7 B
: L: T: F8 g7 [The 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.
) O2 w0 J, W2 S5 a7 k0 g1 f7 |1 M
& [  Y/ c$ ~  ?' r$ g! o% c7 M
# I4 y0 h/ \, e  q* s! s0 E3 l# y% d- Y$ h1 O( N9 x7 S

, J: `3 O3 l' c8 T: p6 ZConcept   n1 |+ H" ]2 ?. l0 [
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.   L. S- `( {# c- P; j

2 v/ F: n. Z2 o9 Q5 |So, 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.
; P& W4 x7 h% I' X# Y! J+ _3 s  [) B) D
Regular setup:  Application <-> d3d9.dll (system) " W$ j. f  W( H. c2 `! k
Proxy setup:  Application <-> d3d9.dll(proxy) <-> d3d9.dll (system) , x& M& W9 |: S& c; B
1 H* E2 W; A7 Z* X3 z
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: 4 d: G3 R2 |$ S/ q* ?0 E

5 ~& B# N8 G0 M) R"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."
' g5 Q. S# M7 k  ?* @) l+ U( wThe 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. . e2 w' J2 Z# s  c% o5 y
$ h" |% m; p" \" ^% E- @9 S6 g9 ]
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).
' y: i7 t  M' l, N4 j1 E
  {! }, |1 ?" Z: w# FRealisation 9 b* D! n8 D$ W, f
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.
4 F1 Z% s9 S4 Y# `# D! A1 N+ D. O& x* U- J. S
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. / ^6 e- T9 E( j: W+ x$ z
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. % _  B9 |. D8 {: G+ D% {

5 x; N0 x& E% l4 xIf 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.
0 v7 x+ S. S* m/ \7 k
( p. }; g# D) F; \. D5 g5 E8 N 2 k; K) j9 y4 l* e! Q5 ~: _
' t, C4 P; g. v* c. g3 D1 r7 ~4 B
Any drawbacks on that? Yes. ) l) d& \# R4 D3 ?9 [' H8 {) a

1 E3 s( h0 {) mNormally, 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.
, J/ r' o- F$ C; o) m& c1 r9 n1 p" c% T( J
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. . J7 Z/ j3 s% N3 ~$ u. t

8 ^: `7 K, Y" F. O8 m" b* o7 L9 LFinally, you know what you need:
: u$ j1 {; h/ d9 g! t! n9 ]2 m
& Q, O2 U0 [9 Ra file named d3d8/d3d9.dll that will be placed within the application's working directory (and hopefully get loaded by the app)
2 T2 s1 y. k( ^; O8 a; wa function named "Direct3DCreate8/9" being exposed by that DLL
- @+ h  b6 L/ g' San interface provided by the DLL that looks exactly the same as the "real" IDirect3D interface
1 @8 m9 N8 E8 B+ v7 U8 n4 dan interface provided by the DLL that looks exactly the same as the "real" IDirect3DDevice interface 3 J* J  t9 A  N# p% o* h
Luckily enough, some header files that come with the DX SDK point out what calls should be handled by your "stub" Interfaces.
4 m8 ]) o6 V4 H( b8 C/ [2 m3 N* }
/ `$ q0 m" M: L# b! X0 C" PThe Demo Project
  n* L5 x. l  B! r+ XMy "basic proxy DLL" implementation consists of these items: 8 ?. `) t  m' v% ]2 b& U2 b& M

6 T( D8 |$ O: h8 Bglobal routines for the DLL handling (entry point, load the system d3d8/d3d9.dll, and so on)
( T. O  h7 R+ f- I- {, a$ Nan object called myIDirect3D8/9, derived from Direct3D8/9, exposing over a dozen functions
2 N; g, z' g) `  {  K" _: y5 g% v: Ban object called myIDirect3DDevice8/9, derived from IDirect3DDevice8/9, exposing about 100 functions 5 d& |; A7 v- I; q# X* z9 w2 Y+ Y9 f& v
Basically, 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.
* i# }# V& c# ^: c3 K
% v' \0 r; V" z, B& R( N::Release (both Objects) 9 d9 `9 _' W. b# K' O! f
In case the application releases the interface (and no more references are present), you destroy your own object as well.
/ N  `: Q- Z% A4 q! E3 W0 ?+ N+ T7 w0 F' n0 p6 k/ l0 b
::QueryInterface (both Objects) 8 r$ l; t" i' q! B/ |- e) u+ F
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. 7 V+ }" {9 t; S/ @7 G. c
- f/ O# n3 d# R
myIDirect3D9::CreateDevice
' X5 e8 m/ d& i( n% w" K0 x& |1 TOnce 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. 0 y; O+ ]1 g) j

8 I! C8 t  G5 N% L1 ZFor 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.
4 H" E: V0 V3 E' Y/ }' n0 R9 e3 H  Z: |  C, b* U
This is a compilation from my work on http://www.proxy.mikoweb.de : d2 ~" F# @9 r! f# h2 {
rev1.1: added DX8 source, corrected typos
: ?0 `* V. t4 H" M' D- \) j) w8 ~, r
About the Author
4 n% `8 n& U) H; D( ]/ HI'm a hobby programmer, using VC++ .net 2003
2 R) M. `( U1 P/ z. r. C& ]0 C$ X, M  e- K: q: O) `8 G8 R! H
Downloads
7 b4 N' [/ C) Z* ~2 V4 }
- g) F* @. ]1 F5 |* U0 V8 F* a7 q2 C! J/ k" {1 K: q9 C' }
proxydll_8_src.zip - Demo Project (d3d8.dll proxy, Visual C++ 2003)
% t! L# j2 H  ]/ P$ g+ A4 }1 x7 }1 S$ K3 |# ~  P: T8 X. p
proxydll_9_src.zip - Demo Project (d3d9.dll proxy, Visual C++ 2003)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 很美好很美好 很差劲很差劲
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

冒险解谜游戏中文网 ChinaAVG

官方微博官方微信号小黑屋 微信玩家群  

(C) ChinaAVG 2004 - 2019 All Right Reserved. Powered by Discuz! X3.2
辽ICP备11008827号 | 桂公网安备 45010702000051号

冒险,与你同在。 冒险解谜游戏中文网ChinaAVG诞生于2004年9月9日,是全球华人共同的冒险解谜类游戏家园。我们致力于提供各类冒险游戏资讯供大家学习交流。本站所有资源均不用于商业用途。

快速回复 返回顶部 返回列表