时间:2019-03-08 标签:c++d3dhooking-COMvtable

标签 c++ vtable hook detours direct3d

尝试制作一个 Fraps 类型的程序。请参阅评论以了解失败的地方。

#include "precompiled.h"

typedef IDirect3D9* (STDMETHODCALLTYPE* Direct3DCreate9_t)(UINT SDKVersion);
Direct3DCreate9_t RealDirect3DCreate9 = NULL;

typedef HRESULT (STDMETHODCALLTYPE* CreateDevice_t)(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
    DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
    IDirect3DDevice9** ppReturnedDeviceInterface);
CreateDevice_t RealD3D9CreateDevice = NULL;

HRESULT STDMETHODCALLTYPE HookedD3D9CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
    DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
    IDirect3DDevice9** ppReturnedDeviceInterface)
{
    // this call makes it jump to HookedDirect3DCreate9 and crashes. i'm doing something wrong
    HRESULT ret = RealD3D9CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags,
        pPresentationParameters, ppReturnedDeviceInterface);

    return ret;
}

IDirect3D9* STDMETHODCALLTYPE HookedDirect3DCreate9(UINT SDKVersion)
{
    MessageBox(0, L"Creating d3d", L"", 0);

    IDirect3D9* d3d = RealDirect3DCreate9(SDKVersion);

    UINT_PTR* pVTable = (UINT_PTR*)(*((UINT_PTR*)d3d));
    RealD3D9CreateDevice = (CreateDevice_t)pVTable[16];

    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)RealD3D9CreateDevice, HookedD3D9CreateDevice);
    if (DetourTransactionCommit() != ERROR_SUCCESS)
    {
        MessageBox(0, L"failed to create createdev hook", L"", 0);
    }

    return d3d;
}

bool APIENTRY DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID lpReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH)
    {
        MessageBox(0, L"", L"", 0);

        RealDirect3DCreate9 = (Direct3DCreate9_t)GetProcAddress(GetModuleHandle(L"d3d9.dll"), "Direct3DCreate9");

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)RealDirect3DCreate9, HookedDirect3DCreate9);
        DetourTransactionCommit();
    }

    // TODO detach hooks

    return true;
}

最佳答案

IDirect3D9::CreateDevice 的 C 接口(interface)的签名是:

STDMETHOD(CreateDevice)(
    THIS_ 
    UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,
    DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,
    IDirect3DDevice9** ppReturnedDeviceInterface) PURE;

扩展为:

typedef HRESULT (STDMETHODCALLTYPE* CreateDevice_t)(
    IDirect3D9 FAR *This, // you forgot this.
    UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, 
    DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, 
    IDirect3DDevice9** ppReturnedDeviceInterface); 

换句话说,您错误地为 CreateDevice 声明了 thunk。

此外,您可能只想#define CINTERFACE 并通过d3d- 访问您想要覆盖的函数,而不是直接索引到IDirect3D9 虚表中>lpVtbl->CreateDevice.

关于时间:2019-03-08 标签:c++d3dhooking-COMvtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2357879/

相关文章:

c++ - 如何总结具有相同键的c++中多重映射中的所有元素

c++ - 从函数返回 thread_local 引用是否合法?

c++ - 缺少 v 表。是什么原因?

c# - 虚函数在 C# 和 Java 中如何工作?

c++ - Qt 链接器错误 : "undefined reference to vtable"

c++ - std::map clear() 崩溃 - 多线程

Ruby:如何通过继承 Hook 回调

windows - 用于保护驱动程序的注册表访问 Hook

windows - 如何在内核模式 Hook 中获取调用者 SID? ( Windows )

c++ - 如何删除 QStandardItemModel 中的垂直标题?