HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Direct3D9Ex.cpp
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#include "Direct3D9Ex.h"
26#include "Exceptions.hpp"
27
28#include <wrl/client.h>
29
30using namespace HydraHook::Core::Exceptions;
31using Microsoft::WRL::ComPtr;
32
34 d3d9_ex(nullptr), d3d9_device_ex(nullptr)
35{
36 temp_window = std::make_unique<Window>("TempDirect3D9ExOverlayWindow");
37
38 const auto hMod = GetModuleHandle("d3d9.dll");
39
40 if (hMod == nullptr)
41 {
42 throw ModuleNotFoundException("Could not get the handle to d3d9.dll");
43 }
44
45 const auto Direct3DCreate9Ex = static_cast<LPVOID>(GetProcAddress(hMod, "Direct3DCreate9Ex"));
46 if (Direct3DCreate9Ex == nullptr)
47 {
48 throw RuntimeException("Could not locate the Direct3DCreate9Ex procedure entry point");
49 }
50
51 ComPtr<IDirect3D9Ex> d3d9;
52 auto error_code = static_cast<HRESULT(WINAPI *)(UINT, IDirect3D9Ex**)>(Direct3DCreate9Ex
53 )(D3D_SDK_VERSION, d3d9.GetAddressOf());
54 if (FAILED(error_code))
55 {
56 throw DXAPIException("Could not create the DirectX 9 interface", error_code);
57 }
58
59 D3DDISPLAYMODE display_mode;
60 error_code = d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode);
61 if (FAILED(error_code))
62 {
63 throw DXAPIException("Could not determine the current display mode", error_code);
64 }
65
66 D3DPRESENT_PARAMETERS present_parameters;
67 ZeroMemory(&present_parameters, sizeof(D3DPRESENT_PARAMETERS));
68 present_parameters.Windowed = TRUE;
69 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
70 present_parameters.BackBufferFormat = display_mode.Format;
71
72 ComPtr<IDirect3DDevice9Ex> device;
73 error_code = d3d9->CreateDeviceEx(
74 D3DADAPTER_DEFAULT,
75 D3DDEVTYPE_HAL,
76 temp_window->windowHandle(),
77 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_DISABLE_DRIVER_MANAGEMENT,
78 &present_parameters,
79 nullptr,
80 device.GetAddressOf()
81 );
82 if (FAILED(error_code))
83 {
84 throw DXAPIException("Could not create the Direct3D 9 device", error_code);
85 }
86
87 d3d9_ex = d3d9.Detach();
88 d3d9_device_ex = device.Detach();
89}
90
91std::vector<size_t> Direct3D9Hooking::Direct3D9Ex::vtable() const
92{
93 return std::vector<size_t>(*reinterpret_cast<size_t**>(d3d9_device_ex),
94 *reinterpret_cast<size_t**>(d3d9_device_ex) + VTableElements);
95}
96
98{
99 if (d3d9_device_ex)
100 d3d9_device_ex->Release();
101
102 if (d3d9_ex)
103 d3d9_ex->Release();
104}
Direct3D 9Ex device vtable indices (extends Direct3D9).
HydraHook exception types (Detours, Win32, generic).
std::vector< size_t > vtable() const override
Returns vtable indices for hookable methods (Present, Reset, etc.).
static const int VTableElements
Definition Direct3D9Ex.h:64
std::unique_ptr< Window > temp_window
DXAPIException(const std::string &msg, HRESULT result)