Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 cycrow 1
// Direct3D-Hook.cpp : Defines the entry point for the DLL application.
2
//
3
 
4
#include "../stdafx.h"
5
#include "apihijack.h"
6
#include "Direct3D-Hook.h"
7
#include "MyDirect3D9.h"
8
#include "../X3Overlay.h"
9
#include "MyDirectInput8.h"
10
#include "../Render/Display.h"
11
 
12
#ifdef _MANAGED
13
#pragma managed(push, off)
14
#endif
15
 
16
HINSTANCE hDLL;
17
 
18
// Function pointer types.
19
typedef IDirect3D9* (WINAPI *Direct3DCreate9_t)(UINT sdk_version);
20
typedef HRESULT (WINAPI *DirectInput8Create_t)(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf,
21
										LPVOID * ppvOut, LPUNKNOWN punkOuter);
22
 
23
// Function prototypes.
24
IDirect3D9* WINAPI MyDirect3DCreate9(UINT sdk_version);
25
HRESULT WINAPI MyDirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID * ppvOut,
26
									LPUNKNOWN punkOuter);
27
 
28
 
29
COverlay *pOverlay = new CX3Overlay;
30
 
31
// Hook structure.
32
enum
33
{
34
	D3DFN_Direct3DCreate9 = 0
35
};
36
 
37
enum
38
{
39
	D3DFN_DirectInput8Create = 0
40
};
41
 
42
 
43
SDLLHook D3DHook = 
44
{
45
	"D3D9.DLL",
46
	false, NULL,		// Default hook disabled, NULL function pointer.
47
	{
48
		{ "Direct3DCreate9", MyDirect3DCreate9},
49
		{ NULL, NULL }
50
	}
51
};
52
 
53
SDLLHook DInputHook = 
54
{
55
	"DINPUT8.DLL",
56
	false, NULL,		// Default hook disabled, NULL function pointer.
57
	{
58
		{ "DirectInput8Create", MyDirectInput8Create},
59
		{ NULL, NULL }
60
	}
61
};
62
 
63
// Hook function.
64
HRESULT WINAPI MyDirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID * ppvOut,
65
									LPUNKNOWN punkOuter)
66
{
67
	// Let the world know we're working.
68
	MessageBeep(MB_ICONINFORMATION);
69
 
70
	OutputDebugString( "DirectInput-Hook: MyDirectInput8Create called.\n" );
71
 
72
	DirectInput8Create_t old_func = reinterpret_cast<DirectInput8Create_t>(DInputHook.Functions[D3DFN_DirectInput8Create].OrigFn);
73
	HRESULT hr = old_func(hinst, dwVersion, riidltf, ppvOut, punkOuter);
74
 
75
	if(SUCCEEDED(hr))
76
		*ppvOut = new MyDirectInput8(reinterpret_cast<IDirectInput8*>(*ppvOut), pOverlay);
77
 
78
	return hr;
79
}
80
 
81
 
82
// Hook function.
83
IDirect3D9* WINAPI MyDirect3DCreate9(UINT sdk_version)
84
{
85
	// Let the world know we're working.
86
	MessageBeep(MB_ICONINFORMATION);
87
 
88
	OutputDebugString( "Direct3D-Hook: MyDirect3DCreate9 called.\n" );
89
 
90
	Direct3DCreate9_t old_func = (Direct3DCreate9_t) D3DHook.Functions[D3DFN_Direct3DCreate9].OrigFn;
91
	IDirect3D9* d3d = old_func(sdk_version);
92
 
93
	return d3d? new MyDirect3D9(d3d, pOverlay) : 0;
94
}
95
 
96
BOOL APIENTRY DllMain( HMODULE hModule,
97
					   DWORD  ul_reason_for_call,
98
					   LPVOID lpReserved
99
					 )
100
{
101
	if(ul_reason_for_call == DLL_PROCESS_ATTACH)  // When initializing....
102
	{
103
		hDLL = hModule;
104
		// We don't need thread notifications for what we're doing.  Thus, get
105
		// rid of them, thereby eliminating some of the overhead of this DLL
106
		DisableThreadLibraryCalls( hModule );
107
 
108
		char targetProcess[512];
109
		DWORD count = 512;
110
 
111
		HKEY appKey = 0;
112
		if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Egosoft\\XUniverseGame-Hook", 0, KEY_QUERY_VALUE,
113
			&appKey))
114
		{
115
			if(ERROR_SUCCESS == RegQueryValueEx(appKey, 0, 0, 0, reinterpret_cast<BYTE*>(targetProcess), &count))
116
			{
117
				char process[512];
118
				GetModuleFileName(GetModuleHandle(0), process, sizeof(process));
119
				PathStripPath(process);
120
 
121
				char logDir[1024];
122
				DWORD logCount = 1024;
123
				if(ERROR_SUCCESS == RegQueryValueEx(appKey, "LogDir", 0, 0, reinterpret_cast<BYTE*>(logDir), &logCount))
124
					((CX3Overlay *)pOverlay)->SetLogDir(logDir);
125
				if(ERROR_SUCCESS == RegQueryValueEx(appKey, "GameDir", 0, 0, reinterpret_cast<BYTE*>(logDir), &logCount))
126
					((CX3Overlay *)pOverlay)->SetGameDir(logDir);
127
 
128
				if(_strnicmp(targetProcess, process, 512) == 0)
129
				{
130
					HookAPICalls(&D3DHook);
131
					HookAPICalls(&DInputHook);
132
				}
133
			}
134
 
135
			RegCloseKey(appKey);
136
		}
137
	}
138
 
139
	return TRUE;
140
}
141
 
142
// This segment must be defined as SHARED in the .DEF
143
#pragma data_seg (".HookSection")		
144
// Shared instance for all processes.
145
HHOOK hHook = NULL;	
146
#pragma data_seg ()
147
 
148
D3DHOOK_API LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) 
149
{
150
    return CallNextHookEx( hHook, nCode, wParam, lParam); 
151
}
152
 
153
D3DHOOK_API void InstallHook()
154
{
155
    OutputDebugString( "D3DHOOK hook installed.\n" );
156
    hHook = SetWindowsHookEx( WH_CBT, HookProc, hDLL, 0 ); 
157
}
158
 
159
D3DHOOK_API void RemoveHook()
160
{
161
    OutputDebugString( "D3DHOOK hook removed.\n" );
162
    UnhookWindowsHookEx( hHook );
163
}
164
 
165
 
166
#ifdef _MANAGED
167
#pragma managed(pop)
168
#endif
169