Skip to content

Commit

Permalink
Disable automatic installation for "DXGID3D10" hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Oct 31, 2020
1 parent 5aaee19 commit 3f2da83
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 37 deletions.
1 change: 1 addition & 0 deletions res/exports.def
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ EXPORTS

DXGID3D10CreateDevice
DXGID3D10CreateLayeredDevice
DXGID3D10ETWRundown
DXGID3D10GetLayeredDeviceSize
DXGID3D10RegisterLayers

Expand Down
28 changes: 14 additions & 14 deletions source/d3d9/d3d9_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

// The following hooks can only be called when installed as d3d9.dll, since they have no names in the export table, just ordinals
// Therefore can assume that the export module handle points toward the system d3d9.dll
// Note that the hooks below can be called before the CRT is initialized, so keep logic as simple as possible!
extern HMODULE s_export_module_handle;
// Note that the hooks below are sometimes called before the CRT is initialized, so keep logic as simple as possible!
extern HMODULE g_export_module_handle;

HOOK_EXPORT void WINAPI Direct3D9ForceHybridEnumeration(UINT Mode)
{
if (s_export_module_handle != nullptr)
if (g_export_module_handle != nullptr)
{
const FARPROC proc = GetProcAddress(s_export_module_handle, reinterpret_cast<LPCSTR>(16));
const FARPROC proc = GetProcAddress(g_export_module_handle, reinterpret_cast<LPCSTR>(16));
if (proc != nullptr)
reinterpret_cast<decltype(&Direct3D9ForceHybridEnumeration)>(proc)(Mode);
}
Expand All @@ -24,19 +24,19 @@ HOOK_EXPORT void WINAPI Direct3D9ForceHybridEnumeration(UINT Mode)
// This is called when the 'DXMaximizedWindowedMode' compatibility fix is active (by AcLayers.dll shim), via export ordinal 17
HOOK_EXPORT void WINAPI Direct3D9SetMaximizedWindowedModeShim(int Unknown, UINT Mode)
{
if (s_export_module_handle != nullptr)
if (g_export_module_handle != nullptr)
{
const FARPROC proc = GetProcAddress(s_export_module_handle, reinterpret_cast<LPCSTR>(17));
const FARPROC proc = GetProcAddress(g_export_module_handle, reinterpret_cast<LPCSTR>(17));
if (proc != nullptr)
reinterpret_cast<decltype(&Direct3D9SetMaximizedWindowedModeShim)>(proc)(Unknown, Mode);
}
}

HOOK_EXPORT void WINAPI Direct3D9SetSwapEffectUpgradeShim(int Unknown)
{
if (s_export_module_handle != nullptr)
if (g_export_module_handle != nullptr)
{
const FARPROC proc = GetProcAddress(s_export_module_handle, reinterpret_cast<LPCSTR>(18));
const FARPROC proc = GetProcAddress(g_export_module_handle, reinterpret_cast<LPCSTR>(18));
if (proc != nullptr)
reinterpret_cast<decltype(&Direct3D9SetSwapEffectUpgradeShim)>(proc)(Unknown);
}
Expand All @@ -45,9 +45,9 @@ HOOK_EXPORT void WINAPI Direct3D9SetSwapEffectUpgradeShim(int Unknown)
// This is called when the 'D3D9On12Enabler' compatibility fix is active (by AcGenral.dll shim), via export ordinal 19
HOOK_EXPORT void WINAPI Direct3D9Force9on12(int Unknown)
{
if (s_export_module_handle != nullptr)
if (g_export_module_handle != nullptr)
{
const FARPROC proc = GetProcAddress(s_export_module_handle, reinterpret_cast<LPCSTR>(19));
const FARPROC proc = GetProcAddress(g_export_module_handle, reinterpret_cast<LPCSTR>(19));
if (proc != nullptr)
reinterpret_cast<decltype(&Direct3D9Force9on12)>(proc)(Unknown);
}
Expand All @@ -56,9 +56,9 @@ HOOK_EXPORT void WINAPI Direct3D9Force9on12(int Unknown)
// This is called when the 'DXMaximizedWindowedHwndOverride' compatibility fix is active (by AcLayers.dll shim), via export ordinal 22
HOOK_EXPORT void WINAPI Direct3D9SetMaximizedWindowHwndOverride(int Unknown)
{
if (s_export_module_handle != nullptr)
if (g_export_module_handle != nullptr)
{
const FARPROC proc = GetProcAddress(s_export_module_handle, reinterpret_cast<LPCSTR>(22));
const FARPROC proc = GetProcAddress(g_export_module_handle, reinterpret_cast<LPCSTR>(22));
if (proc != nullptr)
reinterpret_cast<decltype(&Direct3D9SetMaximizedWindowHwndOverride)>(proc)(Unknown);
}
Expand All @@ -67,9 +67,9 @@ HOOK_EXPORT void WINAPI Direct3D9SetMaximizedWindowHwndOverride(int Unknown)
// This is called when the 'D3D9On12VendorIDLie' compatibility fix is active (by AcGenral.dll shim), via export ordinal 23
HOOK_EXPORT void WINAPI Direct3D9SetVendorIDLieFor9on12(int Unknown)
{
if (s_export_module_handle != nullptr)
if (g_export_module_handle != nullptr)
{
const FARPROC proc = GetProcAddress(s_export_module_handle, reinterpret_cast<LPCSTR>(23));
const FARPROC proc = GetProcAddress(g_export_module_handle, reinterpret_cast<LPCSTR>(23));
if (proc != nullptr)
reinterpret_cast<decltype(&Direct3D9SetVendorIDLieFor9on12)>(proc)(Unknown);
}
Expand Down
76 changes: 59 additions & 17 deletions source/dxgi/dxgi_d3d10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,77 @@
#include <dxgi.h>
#include <cassert>

// These are filtered out during hook installation (see hook_manager.cpp)
HOOK_EXPORT HRESULT WINAPI DXGIDumpJournal()
// The following hooks can only be called when installed as dxgi.dll, since they are skipped otherwise (see hook_manager.cpp)
// Therefore can assume that the export module handle points toward the system dxgi.dll
extern HMODULE g_export_module_handle;

HOOK_EXPORT HRESULT WINAPI DXGIDumpJournal(void *pfnCallback)
{
assert(false);
return E_NOTIMPL;
assert(g_export_module_handle != nullptr);

const FARPROC proc = GetProcAddress(g_export_module_handle, "DXGIDumpJournal");
if (proc != nullptr)
return reinterpret_cast<decltype(&DXGIDumpJournal)>(proc)(pfnCallback);
else
return E_NOTIMPL;
}
HOOK_EXPORT HRESULT WINAPI DXGIReportAdapterConfiguration()

HOOK_EXPORT HRESULT WINAPI DXGIReportAdapterConfiguration(void *pAdapterInfo)
{
assert(false);
return E_NOTIMPL;
}
assert(g_export_module_handle != nullptr);

// When the 'DXGICompat' compatibility fix is active (by AcGenral.dll shim), the shim will look for a 'SetAppCompatStringPointer' export in dxgi.dll and call that
// It does ignore the case where that export does not exist though, so do not need to export it here
const FARPROC proc = GetProcAddress(g_export_module_handle, "DXGIReportAdapterConfiguration");
if (proc != nullptr)
return reinterpret_cast<decltype(&DXGIReportAdapterConfiguration)>(proc)(pAdapterInfo);
else
return E_NOTIMPL;
}

// These are actually called internally by the Direct3D driver on some versions of Windows, so just pass them through
HOOK_EXPORT HRESULT WINAPI DXGID3D10CreateDevice(HMODULE hModule, IDXGIFactory *pFactory, IDXGIAdapter *pAdapter, UINT Flags, void *pUnknown, void **ppDevice)
HOOK_EXPORT HRESULT WINAPI DXGID3D10CreateDevice(HMODULE hModule, IDXGIFactory *pFactory, IDXGIAdapter *pAdapter, UINT Flags, const void *pFeatureLevels, UINT FeatureLevels, void **ppDevice)
{
return reshade::hooks::call(DXGID3D10CreateDevice)(hModule, pFactory, pAdapter, Flags, pUnknown, ppDevice);
assert(g_export_module_handle != nullptr);

const FARPROC proc = GetProcAddress(g_export_module_handle, "DXGID3D10CreateDevice");
if (proc != nullptr)
return reinterpret_cast<decltype(&DXGID3D10CreateDevice)>(proc)(hModule, pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice);
else
return E_NOTIMPL; // Starting with Windows 8 these are no longer implemented and always return 'E_NOTIMPL' either way
}
HOOK_EXPORT HRESULT WINAPI DXGID3D10CreateLayeredDevice(void *pUnknown1, void *pUnknown2, void *pUnknown3, void *pUnknown4, void *pUnknown5)

HOOK_EXPORT HRESULT WINAPI DXGID3D10CreateLayeredDevice(IDXGIAdapter *pAdapter, UINT Flags, void *pUnknown, REFIID riid, void **ppDevice)
{
return reshade::hooks::call(DXGID3D10CreateLayeredDevice)(pUnknown1, pUnknown2, pUnknown3, pUnknown4, pUnknown5);
assert(g_export_module_handle != nullptr);

const FARPROC proc = GetProcAddress(g_export_module_handle, "DXGID3D10CreateLayeredDevice");
if (proc != nullptr)
return reinterpret_cast<decltype(&DXGID3D10CreateLayeredDevice)>(proc)(pAdapter, Flags, pUnknown, riid, ppDevice);
else
return E_NOTIMPL;
}
HOOK_EXPORT SIZE_T WINAPI DXGID3D10GetLayeredDeviceSize(const void *pLayers, UINT NumLayers)

HOOK_EXPORT void WINAPI DXGID3D10ETWRundown()
{
return reshade::hooks::call(DXGID3D10GetLayeredDeviceSize)(pLayers, NumLayers);
}

HOOK_EXPORT HRESULT WINAPI DXGID3D10GetLayeredDeviceSize(const void *pLayers, UINT NumLayers)
{
assert(g_export_module_handle != nullptr);

const FARPROC proc = GetProcAddress(g_export_module_handle, "DXGID3D10GetLayeredDeviceSize");
if (proc != nullptr)
return reinterpret_cast<decltype(&DXGID3D10GetLayeredDeviceSize)>(proc)(pLayers, NumLayers);
else
return E_NOTIMPL;
}

HOOK_EXPORT HRESULT WINAPI DXGID3D10RegisterLayers(const void *pLayers, UINT NumLayers)
{
return reshade::hooks::call(DXGID3D10RegisterLayers)(pLayers, NumLayers);
assert(g_export_module_handle != nullptr);

const FARPROC proc = GetProcAddress(g_export_module_handle, "DXGID3D10RegisterLayers");
if (proc != nullptr)
return reinterpret_cast<decltype(&DXGID3D10RegisterLayers)>(proc)(pLayers, NumLayers);
else
return E_NOTIMPL;
}
17 changes: 11 additions & 6 deletions source/hook_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace
}

extern HMODULE g_module_handle;
HMODULE s_export_module_handle = nullptr;
HMODULE g_export_module_handle = nullptr;
extern std::filesystem::path g_reshade_dll_path;
static std::filesystem::path s_export_hook_path;
static std::vector<std::filesystem::path> s_delayed_hook_paths;
Expand Down Expand Up @@ -170,6 +170,11 @@ static bool install_internal(HMODULE target_module, HMODULE replacement_module,
if (it != replacement_exports.cend() &&
std::strcmp(symbol.name, "DXGIDumpJournal") != 0 &&
std::strcmp(symbol.name, "DXGIReportAdapterConfiguration") != 0 &&
std::strcmp(symbol.name, "DXGID3D10CreateDevice") != 0 &&
std::strcmp(symbol.name, "DXGID3D10CreateLayeredDevice") != 0 &&
std::strcmp(symbol.name, "DXGID3D10ETWRundown") != 0 &&
std::strcmp(symbol.name, "DXGID3D10GetLayeredDeviceSize") != 0 &&
std::strcmp(symbol.name, "DXGID3D10RegisterLayers") != 0 &&
std::strcmp(symbol.name, "Direct3D9EnableMaximizedWindowedModeShim") != 0)
{
#if RESHADE_VERBOSE_LOG
Expand Down Expand Up @@ -400,9 +405,9 @@ void reshade::hooks::uninstall()

// Free reference to the module loaded for export hooks
// Otherwise a subsequent call to 'LoadLibrary' could return the handle to the still loaded export module, instead of loading the ReShade module again
if (s_export_module_handle)
FreeLibrary(s_export_module_handle);
s_export_module_handle = nullptr;
if (g_export_module_handle)
FreeLibrary(g_export_module_handle);
g_export_module_handle = nullptr;
}

void reshade::hooks::register_module(const std::filesystem::path &target_path)
Expand Down Expand Up @@ -457,7 +462,7 @@ reshade::hook::address reshade::hooks::call(hook::address replacement, hook::add
return hook.call();

// If the hook does not exist yet, delay-load export hooks and try again
if (!s_export_module_handle && !s_export_hook_path.empty())
if (!g_export_module_handle && !s_export_hook_path.empty())
{
assert(s_export_hook_path.is_absolute());

Expand All @@ -472,7 +477,7 @@ reshade::hook::address reshade::hooks::call(hook::address replacement, hook::add
install_internal(handle, g_module_handle, hook_method::export_hook);

s_export_hook_path.clear();
s_export_module_handle = handle;
g_export_module_handle = handle;

return call(replacement, target);
}
Expand Down

0 comments on commit 3f2da83

Please sign in to comment.