]> git.cworth.org Git - apitrace/blob - d3d9/dllmain.cpp
Dump flags.
[apitrace] / d3d9 / dllmain.cpp
1 // proxydll.cpp
2 #include "stdafx.h"
3 #include "dllmain.hpp"
4
5 // global variables
6 #pragma data_seg (".d3d9_shared")
7 TraceDirect3DSwapChain9* gl_pmyIDirect3DSwapChain9;
8 TraceDirect3DDevice9* gl_pmyIDirect3DDevice9;
9 TraceDirect3D9* gl_pmyIDirect3D9;
10 HINSTANCE gl_hOriginalDll;
11 HINSTANCE gl_hThisInstance;
12 #pragma data_seg ()
13
14 BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
15 {
16     // to avoid compiler lvl4 warnings 
17     LPVOID lpDummy = lpReserved;
18     lpDummy = NULL;
19
20     switch (ul_reason_for_call)
21     {
22         case DLL_PROCESS_ATTACH: InitInstance(hModule); break;
23         case DLL_PROCESS_DETACH: ExitInstance(); break;
24
25         case DLL_THREAD_ATTACH: break;
26         case DLL_THREAD_DETACH: break;
27     }
28     return TRUE;
29 }
30
31 // Exported function (faking d3d9.dll's one-and-only export)
32 IDirect3D9* WINAPI Direct3DCreate9(UINT SDKVersion)
33 {
34     if (!gl_hOriginalDll) LoadOriginalDll(); // looking for the "right d3d9.dll"
35
36     // Hooking IDirect3D Object from Original Library
37     typedef IDirect3D9 *(WINAPI* D3D9_Type)(UINT SDKVersion);
38     D3D9_Type D3DCreate9_fn = (D3D9_Type) GetProcAddress( gl_hOriginalDll, "Direct3DCreate9");
39
40     // Debug
41     if (!D3DCreate9_fn)
42     {
43         OutputDebugString(_T("PROXYDLL: Pointer to original D3DCreate9 function not received ERROR ****\r\n"));
44         ::ExitProcess(0); // exit the hard way
45     }
46
47     // Request pointer from Original Dll. 
48     IDirect3D9 *pIDirect3D9_orig = D3DCreate9_fn(SDKVersion);
49
50     // Create my IDirect3D8 object and store pointer to original object there.
51     // note: the object will delete itself once Ref count is zero (similar to COM objects)
52     gl_pmyIDirect3D9 = new TraceDirect3D9(pIDirect3D9_orig);
53
54     // Return pointer to hooking Object instead of "real one"
55     return (gl_pmyIDirect3D9);
56 }
57
58 void InitInstance(HANDLE hModule) {
59     OutputDebugString(_T("PROXYDLL: InitInstance called.\r\n"));
60
61     // Initialisation
62     gl_hOriginalDll = NULL;
63     gl_hThisInstance = NULL;
64     gl_pmyIDirect3D9 = NULL;
65     gl_pmyIDirect3DDevice9 = NULL;
66     gl_pmyIDirect3DSwapChain9 = NULL;
67
68     // Storing Instance handle into global var
69     gl_hThisInstance = (HINSTANCE) hModule;
70 }
71
72 void LoadOriginalDll(void) {
73     TCHAR buffer[MAX_PATH];
74
75     // Getting path to system dir and to d3d8.dll
76     ::GetSystemDirectory(buffer, MAX_PATH);
77
78     // Append dll name
79     _tcscat(buffer, _T("\\d3d9.dll"));
80
81     // try to load the system's d3d9.dll, if pointer empty
82     if (!gl_hOriginalDll)
83         gl_hOriginalDll = ::LoadLibrary(buffer);
84
85     // Debug
86     if (!gl_hOriginalDll) {
87         OutputDebugString(_T("PROXYDLL: Original d3d9.dll not loaded ERROR ****\r\n"));
88         ::ExitProcess(0); // exit the hard way
89     }
90 }
91
92 void ExitInstance() {
93     OutputDebugString(_T("PROXYDLL: ExitInstance called.\r\n"));
94
95     // Release the system's d3d9.dll
96     if (gl_hOriginalDll) {
97         ::FreeLibrary(gl_hOriginalDll);
98         gl_hOriginalDll = NULL;
99     }
100 }
101