HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Direct3D10.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 "Direct3D10.h"
26#include <vector>
27#include <wrl/client.h>
28#include <dxgi1_4.h>
29#include "DXGI.h"
30#include "Exceptions.hpp"
31
32using namespace HydraHook::Core::Exceptions;
33using Microsoft::WRL::ComPtr;
34
36 pDevice(nullptr), pSwapChain(nullptr)
37{
38 const auto hModDXGI = GetModuleHandle("DXGI.dll");
39 const auto hModD3D10 = GetModuleHandle("D3D10.dll");
40
41 if (hModDXGI == nullptr)
42 {
43 throw ModuleNotFoundException("Could not get handle to DXGI.dll");
44 }
45
46 if (hModD3D10 == nullptr)
47 {
48 throw ModuleNotFoundException("Could not get handle to D3D10.dll");
49 }
50
51 const auto hCreateDXGIFactory = static_cast<LPVOID>(GetProcAddress(hModDXGI, "CreateDXGIFactory"));
52 if (hCreateDXGIFactory == nullptr)
53 {
54 throw ProcAddressNotFoundException("Could not get handle to CreateDXGIFactory");
55 }
56
57 ComPtr<IDXGIFactory> pFactory;
58 const auto hr = static_cast<HRESULT(WINAPI *)(
59 REFIID,
60 void** ppFactory)>(hCreateDXGIFactory)(
61 __uuidof(IDXGIFactory),
62 reinterpret_cast<void**>(pFactory.GetAddressOf()));
63
64 if (FAILED(hr))
65 {
66 throw DXAPIException("Could not create DXGI factory", hr);
67 }
68
69 const auto hD3D10CreateDeviceAndSwapChain = static_cast<LPVOID>(GetProcAddress(
70 hModD3D10,
71 "D3D10CreateDeviceAndSwapChain"));
72 if (hD3D10CreateDeviceAndSwapChain == nullptr)
73 {
74 throw ProcAddressNotFoundException("Could not get handle to D3D10CreateDeviceAndSwapChain");
75 }
76
77 UINT i = 0;
78 ComPtr<IDXGIAdapter> pAdapter;
79 std::vector<ComPtr<IDXGIAdapter>> vAdapters;
80 while (pFactory->EnumAdapters(i, pAdapter.ReleaseAndGetAddressOf()) != DXGI_ERROR_NOT_FOUND)
81 {
82 vAdapters.push_back(pAdapter);
83 ++i;
84 }
85
86 if (vAdapters.empty())
87 {
88 throw RuntimeException("No adapters found");
89 }
90
91 temp_window = std::make_unique<Window>("TempDirect3D10OverlayWindow");
92
93 DXGI_RATIONAL rational = {};
94 rational.Numerator = 60;
95 rational.Denominator = 1;
96
97 DXGI_MODE_DESC modeDesc = {};
98 modeDesc.Height = 100;
99 modeDesc.Width = 100;
100 modeDesc.RefreshRate = rational;
101 modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
102
103 DXGI_SAMPLE_DESC sampleDesc = {};
104 sampleDesc.Count = 1;
105 sampleDesc.Quality = 0;
106
107 DXGI_SWAP_CHAIN_DESC scDesc = {};
108 scDesc.BufferCount = 1;
109 scDesc.Flags = 0;
110 scDesc.Windowed = true;
111 scDesc.OutputWindow = temp_window->windowHandle();
112 scDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
113 scDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
114 scDesc.SampleDesc = sampleDesc;
115 scDesc.BufferDesc = modeDesc;
116
117 const auto hr10 = static_cast<HRESULT(WINAPI *)(
118 IDXGIAdapter*,
119 D3D10_DRIVER_TYPE,
120 HMODULE,
121 UINT,
122 UINT,
123 DXGI_SWAP_CHAIN_DESC*,
124 IDXGISwapChain**,
125 ID3D10Device**)>(hD3D10CreateDeviceAndSwapChain)(
126 vAdapters[0].Get(),
127 D3D10_DRIVER_TYPE_HARDWARE,
128 nullptr,
129 0,
130 D3D10_SDK_VERSION,
131 &scDesc,
132 &pSwapChain,
133 &pDevice);
134
135 if (FAILED(hr10))
136 {
137 throw DXAPIException("Could not create D3D10 device", hr10);
138 }
139}
140
141std::vector<size_t> Direct3D10Hooking::Direct3D10::vtable() const
142{
143 auto capture = [this](int count)
144 {
145 return std::vector<size_t>(*reinterpret_cast<size_t**>(pSwapChain),
146 *reinterpret_cast<size_t**>(pSwapChain) + count);
147 };
148
149 IDXGISwapChain3* swapChain3 = nullptr;
150 if (SUCCEEDED(pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain3))))
151 {
152 swapChain3->Release();
154 }
155
156 IDXGISwapChain1* swapChain1 = nullptr;
157 if (SUCCEEDED(pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain1))))
158 {
159 swapChain1->Release();
161 }
162
164}
165
167{
168 if (pSwapChain)
169 pSwapChain->Release();
170
171 if (pDevice)
172 pDevice->Release();
173}
IDXGISwapChain vtable indices for D3D10/11/12 hooking.
HydraHook exception types (Detours, Win32, generic).
static const int SwapChainVTableElements
Definition DXGI.h:150
static const int SwapChain1VTableElements
Definition DXGI.h:151
static const int SwapChain3VTableElements
Definition DXGI.h:152
std::vector< size_t > vtable() const override
Returns vtable indices for hookable methods (Present, Reset, etc.).
std::unique_ptr< Window > temp_window
DXAPIException(const std::string &msg, HRESULT result)