#include "cInclude.h"
bool Create = false;
HRESULT APIENTRY myPresent(IDirect3DDevice9 * m_pDevice, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
{
if (Create == false)
{
ImGui_Init(hWnd, m_pDevice);
Create = true;
}
else
{
ImGui_NewFrame();
if (bShowWindow)
{
DWORD dwFlag = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_ShowBorders | ImGuiWindowFlags_NoSavedSettings;
ImGui::Begin("legitXXX" , &bShowWindow, ImVec2(243, 200), 1.0f, dwFlag);
{
ImGui::Button("A", ImVec2(100.0f, 30.0f));
ImGui::SameLine();
if (ImGui::Button("D", ImVec2(100.0f, 30.0f)))
{
ImGui::RadioButton("A", true);
ImGui::RadioButton("A", false);
}
}
ImGui::End();
}
ImGui::Render();
}
return oPresent->GetTrampoline()(m_pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
}
HRESULT APIENTRY myReset(IDirect3DDevice9* m_pDevice, D3DPRESENT_PARAMETERS *pPresentationParameters)
{
if (!Create)
return m_pDevice->Reset(pPresentationParameters);
ImGui_InvalidateDeviceObjects();
auto result = oReset->GetTrampoline()(m_pDevice, pPresentationParameters);
ImGui_CreateDeviceObjects();
return result;
}
bool Init()
{
bool bResult = false;
HMODULE hD3d9 = NULL;
if (hD3d9 = GetModuleHandleA("d3d9.dll"))
{
using oDirect3DCreate9Ex = HRESULT(WINAPI*)(UINT, IDirect3D9Ex**);
oDirect3DCreate9Ex pDirect3DCreate9Ex = (oDirect3DCreate9Ex)GetProcAddress(hD3d9, "Direct3DCreate9Ex");
if (pDirect3DCreate9Ex)
{
HRESULT hr = D3D_OK;
LPDIRECT3D9EX d3d9ex = nullptr;
if (SUCCEEDED(hr = pDirect3DCreate9Ex(D3D_SDK_VERSION, &d3d9ex)))
{
D3DPRESENT_PARAMETERS dp;
ZeroMemory(&dp, sizeof(dp));
dp.Windowed = 1;
dp.SwapEffect = D3DSWAPEFFECT_FLIP;
dp.BackBufferFormat = D3DFMT_A8R8G8B8;
dp.BackBufferCount = 1;
dp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
IDirect3DDevice9Ex *mDevice = nullptr;
if (SUCCEEDED(hr = d3d9ex->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dp, NULL, &mDevice)))
{
bResult = true;
PVOID* vtbl = *reinterpret_cast<PVOID**>(mDevice);
auto& pContext = cContext::GetInstance();
pContext.ApplyDetour<PresentFn>(static_cast<PresentFn>(vtbl[17]), reinterpret_cast<PresentFn>(myPresent), &oPresent);
pContext.ApplyDetour<ResetFn>(static_cast<ResetFn>(vtbl[16]), reinterpret_cast<ResetFn>(myReset), &oReset);
mDevice->Release();
}
d3d9ex->Release();
}
}
}
return bResult;
}
unsigned WINAPI GUIDX(LPVOID lpParam)
{
hWnd = FindWindowA(NULL,"D3D9 Test");
if (hWnd)
m_pWindowProc = (WNDPROC)SetWindowLongPtr(hWnd, GWL_WNDPROC, (LONG_PTR)myWndProc);
while (!Init())
Sleep(200);
return 0L;
}
BOOL APIENTRY DllMain(
_In_ HINSTANCE hinstDLL,
_In_ DWORD fdwReason,
_In_ LPVOID lpvReserved) {
DisableThreadLibraryCalls(hinstDLL);
switch (fdwReason) {
case DLL_PROCESS_ATTACH: {
_beginthreadex(NULL, NULL, GUIDX, NULL, NULL, NULL);
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
SetWindowLongPtr(hWnd, GWL_WNDPROC, (LONG_PTR)m_pWindowProc);
break;
}
return TRUE;
}