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

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

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

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

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

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

http://www.codeguru.com/cpp/g-m/ ... /article.php/c11453
, m% ~) ~- n' x! U1 b! W( k
/ T0 H9 |  J4 w0 t' E. sIntercept Calls to DirectX with a Proxy DLL
9 i0 L0 Q1 K6 \Show own content on top of a full screen DirectX application 1 P  |2 N# r/ Z( i7 |
Rating: none
. s( k; x& w. j1 W; Q( o6 i1 |4 S" g! d
Michael Koch (view profile) + F- |9 F. c; @0 n" F7 R3 f5 I
March 13, 2006  
# D" f8 i8 \$ @( I
& J) O6 d3 [2 uEnvironment:  Windows, DirectX 8/9, Visual C++ 2003 6 C0 B- Z4 Q/ j

, \. r6 d% k' ?8 i/ T6 g( y) T+ Y2 ~  b3 L6 C
: L4 s% @- U; K$ l" u
Introduction
& s2 E0 K% X9 j, u* w( IHave 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)?
8 P+ J6 k% {. }% N; W( p: U
6 p  K" C- B% y( _+ O
7 L6 f6 ?- ~; w3 R1 D. l( c! H(continued)
* u0 Y0 P0 d- E3 Y) f( p  |2 w& j% T' {

1 B& z* D! d" x% z# _4 S
- D# r  [4 j5 D1 B& F
$ Z4 M! K5 b) @# r/ gMARKETPLACE
: Q1 _" z/ _4 b3 N; [4 cFlowcharts from C/C++ code -- Free trial download
4 `. G0 D! p# i8 A$ p, S6 tUnderstand C/C++ code in less time. Flowcharts, DataFlow, Rich Trees, Static Checking, Documentation 9 }& a8 B1 \0 `/ I/ @- E: c
www.sgvsarc.com
/ z* b# ?. t+ X: yNew Webcast: How to Profit with Remote Support.
% x' T' @: A% L5 e* I7 B) [Discover how REMOTE SUPPORT can fuel your IT business in ways you've never thought of before.
- i7 G' U4 f6 [Listen to this valuable Webcast Today! www.LogMeInRescue.com 7 U+ ~2 y7 g% {) s' }
Advertiser Here  ; s4 a+ y8 ]9 _" W1 w# B. A, H
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).   w# ^; j% P/ o2 l7 d) l

( J' M4 }, p; F! T7 g+ ?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.
7 z$ w+ Y& ?$ C2 y. ], ^( _1 l
- S+ x9 E' {5 e4 O: V- u( ]- aThe 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. ) \. `2 l" U2 H" v

& I' f7 [6 l" Q5 X% | 5 p( Z# A/ q- F5 ?1 T: q/ e* Z; r

# S; @5 Z; B; A) |+ W2 p
, H( S9 Y3 ~0 e* s0 A7 xConcept & Z1 O) b- T+ a7 V& A7 H
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.
8 l, \$ b6 k# i
8 G5 C  p7 m/ u$ Y/ B8 ^. 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.
6 U0 x" z; ]& ~7 q$ Q2 @& k4 a' V! I
Regular setup:  Application <-> d3d9.dll (system)
$ X" `5 Q/ H% ?$ Q- ^+ V  sProxy setup:  Application <-> d3d9.dll(proxy) <-> d3d9.dll (system)
/ Q# D  H' x$ m8 h
0 y. j& o( V/ _3 M. H0 ZBy the way, this concept is very similar to one used by a Microsoft DirectX programming utility, named D3DSpy. Quote from the D3DSpy help file:
) Z& ]- @; D% k; h
6 a: y- S' x/ q& Q1 o; l* ?% h0 q"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." 3 ^% T+ j  d! s/ w
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.
' z  J' S3 V, {1 f: h" \, ~
" n" Q' ]: L3 C1 B( R  PThere 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). , ?' w* G6 ?1 S0 W

6 J7 u- L: a+ P  ^$ ]Realisation - {9 A# |% E  l6 t, N
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.
3 i8 p3 O+ z3 k7 b8 ]. Y9 Q" E, d2 {: U2 Y
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. : A; u: ^  y/ D$ i6 h
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.
) U( N/ ?5 Z5 U4 t* G4 g( |7 `0 Z9 S3 U1 V! z% [
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.
9 F1 @$ J/ ]: J; l: @) }
# x( B6 g" s# Y+ i / w0 h2 r& H' w6 Y- h; u9 E

7 D; R9 y6 J/ F: rAny drawbacks on that? Yes.
( x; W; p) E( E, c" w. B5 P# M  b$ N5 a6 n% q+ W) f' r3 D$ K
Normally, 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. " ^4 A- L+ a+ |2 L3 B

( K' i: n/ z, p+ _2 PHaving 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.
  B+ t- n% |4 _: z! C
* i" `, j" }5 k, Z! OFinally, you know what you need:
! d5 k2 C# ]2 Y$ v4 H: Q
; ]2 H( C6 a3 ^1 g& Wa file named d3d8/d3d9.dll that will be placed within the application's working directory (and hopefully get loaded by the app)
1 P* u, ?/ t/ |  J1 N2 X& ma function named "Direct3DCreate8/9" being exposed by that DLL ' ~# |5 _& U  z, T& g$ J
an interface provided by the DLL that looks exactly the same as the "real" IDirect3D interface ( d" T" I  B7 A1 ?' w2 a
an interface provided by the DLL that looks exactly the same as the "real" IDirect3DDevice interface
3 z( K9 V+ {2 v. A3 ^4 K7 U  ?! y8 T7 vLuckily enough, some header files that come with the DX SDK point out what calls should be handled by your "stub" Interfaces.
4 o3 @9 A* _5 P+ f- E, u
( ?$ A, m6 P. j: V8 y- WThe Demo Project
' w$ l# R9 L1 N- y9 Y; QMy "basic proxy DLL" implementation consists of these items:
% |$ a- c: _- Z9 v. m2 T  N* O$ g- G, \  r7 \* B+ C9 [- u
global routines for the DLL handling (entry point, load the system d3d8/d3d9.dll, and so on) * m9 q( H8 X4 s' V  y
an object called myIDirect3D8/9, derived from Direct3D8/9, exposing over a dozen functions
9 x% {& i( m2 I3 Z+ B$ i$ a2 wan object called myIDirect3DDevice8/9, derived from IDirect3DDevice8/9, exposing about 100 functions
8 d$ ^( Q6 A0 I$ R9 r8 a3 lBasically, 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.
9 m0 y( K  |3 o5 G  n! R0 ~. D$ G8 U1 u& s/ |+ X
::Release (both Objects) % V+ s, c% o; n* O) R: [3 ~) a" N
In case the application releases the interface (and no more references are present), you destroy your own object as well.
: {- f; _5 p% N/ f
* T5 Q5 L! Q; {1 P5 c/ \::QueryInterface (both Objects)
; N4 x& ^0 _; A1 oIf 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.
0 [& v3 x& Z- P) X9 Q( a9 U  K
; T1 l1 e" r3 x* BmyIDirect3D9::CreateDevice 1 }+ V0 [( }* |4 C: }  R9 r5 e
Once 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.
% b, s& Q0 e& T7 @+ k8 s; `
1 f6 J+ g7 q. T1 VFor 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. ' T' n' H( r6 `6 ]/ K2 X

* `7 Q) K! ~6 Q: P. T; |3 T5 fThis is a compilation from my work on http://www.proxy.mikoweb.de ' D, T$ P7 Q9 V7 g+ p- o3 T% Q
rev1.1: added DX8 source, corrected typos
' S% F: j3 d8 t2 b3 N8 I7 M% B& h! D' y: f' c( n* l! I8 p' R9 t
About the Author # y$ n$ l5 a) U- K
I'm a hobby programmer, using VC++ .net 2003
6 H. A/ t& O8 d2 n3 J) e+ z: c$ @% V6 @: ~' |0 k
Downloads 8 e" A% x' V6 x3 l" V; v

- {9 R5 m1 e! |: `; B: Y% @4 X% t7 ]3 W9 i$ N) d. ^6 ~
proxydll_8_src.zip - Demo Project (d3d8.dll proxy, Visual C++ 2003)
" \$ t* y  O% Q4 Y( ]1 u/ l; V3 e: |5 S* T. F  [0 T1 J
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日,是全球华人共同的冒险解谜类游戏家园。我们致力于提供各类冒险游戏资讯供大家学习交流。本站所有资源均不用于商业用途。

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