Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sometimes d3d11 hook is being created before the game even loads the dll #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions IHHook/D3D11Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ D3D11Hook::~D3D11Hook() {
bool D3D11Hook::hook() {
spdlog::info("Hooking D3D11");

auto pD3D11CreateDeviceAndSwapChain = (decltype(&::D3D11CreateDeviceAndSwapChain)) GetProcAddress(GetModuleHandleW(L"d3d11.dll"), "D3D11CreateDeviceAndSwapChain");

if (pD3D11CreateDeviceAndSwapChain == nullptr){
spdlog::error("d3d11.dll is not loaded can't hook d3d11 functions");

return m_hooked = false;
}
g_d3d11_hook = this;

HWND h_wnd = GetDesktopWindow();
IDXGISwapChain* swap_chain = nullptr;
ID3D11Device* device = nullptr;
D3D_FEATURE_LEVEL device_max_feature_level = D3D_FEATURE_LEVEL_9_1;
D3D_FEATURE_LEVEL device_max_feature_level;
ID3D11DeviceContext* context = nullptr;

D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0;
D3D_FEATURE_LEVEL feature_level[] = {D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_11_0};
DXGI_SWAP_CHAIN_DESC swap_chain_desc;

ZeroMemory(&swap_chain_desc, sizeof(swap_chain_desc));
Expand All @@ -39,7 +46,7 @@ bool D3D11Hook::hook() {
swap_chain_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
spdlog::info("Creating dummy D3D11 device.");
HRESULT hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, &feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, &device_max_feature_level, &context);
HRESULT hr = pD3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, &device_max_feature_level, &context);
if (FAILED(hr)) {
spdlog::error("Failed to create dummy D3D11 device. HRESULT={0:x} max_feature={1:x}", hr, device_max_feature_level);
return false;
Expand Down
33 changes: 32 additions & 1 deletion IHHook/IHHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ namespace IHHook {
}
}//InitCursorHook

typedef HMODULE(WINAPI* LoadLibraryAFn)(LPCSTR);
LoadLibraryAFn g_LoadLibraryA = NULL;

bool IHH::isD3D11Loaded = false;
HMODULE WINAPI LoadLibraryA_Hook(LPCSTR lpFileName)
{
if (strcmp(lpFileName, "ole32.dll") == 0) //at this point d3d11.dll is loaded by the game
{
spdlog::info("LoadLibraryA_Hook: {:} is loaded", lpFileName);
g_ihhook->isD3D11Loaded = true;
}

return g_LoadLibraryA(lpFileName);
}

void InitLoadLibraryAHook()
{
auto log = spdlog::get("ihhook");

if (MH_CreateHook(&LoadLibraryA, &LoadLibraryA_Hook, reinterpret_cast<LPVOID*>(&g_LoadLibraryA)) != MH_OK)
{
log->error("Couldn't create hook for LoadLibraryA.");
}

if (MH_EnableHook(&LoadLibraryA) != MH_OK)
{
log->error("Couldn't enable LoadLibraryA hook.");
}
}

void Shutdown() {
spdlog::debug("IHHook DLL_PROCESS_DETACH");
doShutDown = true;
Expand Down Expand Up @@ -303,7 +333,7 @@ namespace IHHook {

auto tend = std::chrono::high_resolution_clock::now();
auto durationShort = std::chrono::duration_cast<std::chrono::microseconds>(tend - tstart).count();
spdlog::debug("IHHook::CreateHooks total time(microseconds): {}�s", durationShort);
spdlog::debug("IHHook::CreateHooks total time(microseconds): {}�s", durationShort);
}//if doHooks

PipeServer::StartPipeServer();
Expand Down Expand Up @@ -868,6 +898,7 @@ namespace IHHook {
}//RebaseAddresses

void IHH::CreateAllHooks() {
InitLoadLibraryAHook();
Hooks_CityHash::CreateHooks(RealBaseAddr);//TODO: rebase/convert to same style as rest, so don't have to pass in realbaseaddr
Hooks_FNVHash::CreateHooks();
Hooks_Lua::CreateHooks();
Expand Down
1 change: 1 addition & 0 deletions IHHook/IHHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace IHHook {
void OnReset();

bool OnMessage(HWND wnd, UINT message, WPARAM w_param, LPARAM l_param);
static bool isD3D11Loaded;
private:
void SetupLog();
std::string GetLangVersion();
Expand Down
5 changes: 5 additions & 0 deletions IHHook/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ HMODULE g_thisModule;
extern HMODULE origDll;//dinputproxy

DWORD WINAPI InitThread(LPVOID lpParameter) {

if (g_ihhook->isD3D11Loaded == true){

g_ihhook->Initialize();

}

return 0;
}//InitThread
Expand Down