aboutsummaryrefslogtreecommitdiff
path: root/MemDriverLib/DX11Manager.cpp
blob: ab376aa8acc790d3a205a70566f639d05b0b35d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "stdafx.h"
#include "DX11Manager.h"

#include <Windows.h>

#pragma comment (lib, "D3D11.lib")

struct DDataIntern {
	IDXGISwapChain * SwapChain;
	ID3D11Device * Device;
	ID3D11DeviceContext * DeviceContext;
};

static LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case(WM_DESTROY):
		PostQuitMessage(0);
		return 0;
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
		break;
	}
}

static HRESULT InitD3D(struct DDataIntern * const data, HWND hWnd)
{
	DXGI_SWAP_CHAIN_DESC SwapChainDesc;

	ZeroMemory(&SwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
	SwapChainDesc.BufferCount = 1;
	SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	SwapChainDesc.OutputWindow = hWnd;
	SwapChainDesc.SampleDesc.Count = 4;
	SwapChainDesc.Windowed = true;

	return D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &SwapChainDesc,
		&data->SwapChain, &data->Device, NULL, &data->DeviceContext);
}

static void CleanD3D(struct DDataIntern * const data)
{
	data->SwapChain->Release();
	data->Device->Release();
	data->DeviceContext->Release();
}

bool WINAPI GetDirectxData(struct DxData * const data)
{
	HINSTANCE hInstance = (HINSTANCE)((LONG_PTR)GetWindowLongW(GetActiveWindow(), -6));
	HWND hWnd;
	WNDCLASSEX wc;
	struct DDataIntern data_intern;

	ZeroMemory(&data_intern, sizeof(data_intern));
	ZeroMemory(&wc, sizeof(WNDCLASSEX));
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WinProc;
	wc.lpszClassName = L"DxData";
	wc.style = CS_VREDRAW | CS_HREDRAW;
	RegisterClassEx(&wc);

	RECT rect = { 0, 0, 600, 400 };
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
	hWnd = CreateWindowEx(NULL, L"DxData", L"DxData", WS_OVERLAPPEDWINDOW, 300, 300, rect.right - rect.left,
		rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
	if (!hWnd) {
		return false;
	}

	ShowWindow(hWnd, NULL);
	data->CreateSwapChainReturn = InitD3D(&data_intern, hWnd);

	{
		MSG msg;
		while (GetMessage(&msg, NULL, 0, 0) &&
			PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	memcpy(data->buf, (*(UINT64 ***)data_intern.SwapChain)[8], sizeof data->buf);

	HMODULE d3d11_base = LoadLibrary(L"d3d11.dll");
	data->DeviceVTableOffset = *(UINT64 *)data_intern.Device;
	data->DeviceContextVTableOffset = *(UINT64 *)data_intern.DeviceContext;
	data->SwapChainVTableOffset = (*(UINT64 **)data_intern.SwapChain)[8] - (UINT64)d3d11_base;

	CleanD3D(&data_intern);
	CloseWindow(hWnd);
	DestroyWindow(hWnd);

	{
		MSG msg;
		while (GetMessage(&msg, NULL, 0, 0) &&
			PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	FreeLibrary(d3d11_base);

	return true;
}