簡體   English   中英

覆蓋Direct3D接口?

[英]Overriding Direct3D interfaces?

我的項目啟動了一個目標進程,向其中注入了一個庫,該庫旨在掛鈎Direct3D函數。 我過去已經成功完成了此操作,但決定重寫該庫以使其更符合Direct3D接口的規范。 這里的想法是創建一組我自己的1:1包裝器類,每個類都繼承DirectX接口(即, class CD3D8 : IDirect3D8class CD3DDevice8 : IDirect3DDevice8等)。 由於每個基本DirectX COM接口的所有成員都是純虛擬方法,所以我認為我可以輕松地覆蓋它們...

因此,當我的庫鈎住Direct3DCreate8 ,我將返回一個指向CDirect3D8的指針,而不是標准IDirect3D8 然后我的庫使用該指針的虛擬表來掛鈎方法15,即IDirect3D8::CreateDevice 一旦調用,我將返回一個指向CDirect3DDevice8的指針,而不是IDirect3DDevice8 一切似乎都很好,除非注入的應用程序未調用任何覆蓋的函數! 該應用程序似乎以某種方式正在調用原始接口函數,而不是我自己的接口函數。 我在這里缺少某種概念嗎? 例如,是否需要將虛擬表指針手動重新映射到自定義包裝類中的指針?

這是到目前為止我得到的(僅顯示d3d8):

3D文件

#pragma once


#define STDM(method)        COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE method
#define STDM_(type,method)  COM_DECLSPEC_NOTHROW type STDMETHODCALLTYPE method

template<class IDirect3D8>
class CD3D : public IDirect3D8
{
public:
    CD3D();
    ~CD3D();

    /*** IUnknown methods ***/
    STDM(QueryInterface)(THIS_ REFIID riid, void** ppvObj);
    STDM_(ULONG,AddRef)(THIS);
    STDM_(ULONG,Release)(THIS);

    /*** IDirect3D8 methods ***/
    STDM(RegisterSoftwareDevice)(THIS_ void* pInitializeFunction);
    STDM_(UINT, GetAdapterCount)(THIS);
    STDM(GetAdapterIdentifier)(THIS_ UINT Adapter,DWORD Flags,D3DADAPTER_IDENTIFIER8* pIdentifier);
    STDM_(UINT, GetAdapterModeCount)(THIS_ UINT Adapter);
    STDM(EnumAdapterModes)(THIS_ UINT Adapter,UINT Mode,D3DDISPLAYMODE* pMode);
    STDM(GetAdapterDisplayMode)(THIS_ UINT Adapter,D3DDISPLAYMODE* pMode);
    STDM(CheckDeviceType)(THIS_ UINT Adapter,D3DDEVTYPE CheckType,D3DFORMAT DisplayFormat,D3DFORMAT BackBufferFormat,BOOL Windowed);
    STDM(CheckDeviceFormat)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat);
    STDM(CheckDeviceMultiSampleType)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType);
    STDM(CheckDepthStencilMatch)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat);
    STDM(GetDeviceCaps)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS8* pCaps);
    STDM_(HMONITOR, GetAdapterMonitor)(THIS_ UINT Adapter);
    STDM(CreateDevice)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice8** ppReturnedDeviceInterface);
};


D3D.cpp

#include "stdafx.h"


#define STDIMP(iface, method, ...) COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE CD3D<iface>::method(__VA_ARGS__)
#define STDIMP_(iface, type, method, ...) COM_DECLSPEC_NOTHROW type STDMETHODCALLTYPE CD3D<iface>::method(__VA_ARGS__)
#define STDIMP8(method, ...) STDIMP(IDirect3D8, method, __VA_ARGS__)
#define STDIMP8_(type, method, ...)  STDIMP_(IDirect3D8, type, method, __VA_ARGS__)


CD3D<IDirect3D8>::CD3D()
{
}

CD3D<IDirect3D8>::~CD3D()
{
}

STDIMP8(QueryInterface, THIS_ REFIID riid, void** ppvObj) {
    return QueryInterface( riid, ppvObj );
}

STDIMP8_(ULONG, AddRef, THIS) {
    return AddRef();
}

STDIMP8_(ULONG, Release, THIS) {
    return Release();
}

STDIMP8(RegisterSoftwareDevice, THIS_ void* pInitializeFunction) {
    return RegisterSoftwareDevice( pInitializeFunction );
}

STDIMP8_(UINT, GetAdapterCount, THIS) {
    return GetAdapterCount();
}

STDIMP8(GetAdapterIdentifier, THIS_ UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
    return GetAdapterIdentifier( Adapter, Flags, pIdentifier );
}

STDIMP8_(UINT, GetAdapterModeCount, THIS_ UINT Adapter) {
    return GetAdapterModeCount( Adapter );
}

STDIMP8(EnumAdapterModes, THIS_ UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
    return EnumAdapterModes( Adapter, Mode, pMode );
}

STDIMP8(GetAdapterDisplayMode, THIS_ UINT Adapter, D3DDISPLAYMODE* pMode) {
    return GetAdapterDisplayMode( Adapter, pMode );
}

STDIMP8(CheckDeviceType, THIS_ UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat, D3DFORMAT BackBufferFormat, BOOL Windowed) {
    return CheckDeviceType( Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed );
}

STDIMP8(CheckDeviceFormat, THIS_ UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
    return CheckDeviceFormat( Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat );
}

STDIMP8(CheckDeviceMultiSampleType, THIS_ UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
    return CheckDeviceMultiSampleType( Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType );
}

STDIMP8(CheckDepthStencilMatch, THIS_ UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
    return CheckDepthStencilMatch( Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat );
}

STDIMP8(GetDeviceCaps, THIS_ UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
    return GetDeviceCaps( Adapter, DeviceType, pCaps );
}

STDIMP8_(HMONITOR, GetAdapterMonitor, THIS_ UINT Adapter) {
    return GetAdapterMonitor( Adapter );
}

STDIMP8(CreateDevice, THIS_ UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice8** ppReturnedDeviceInterface) {
    return CreateDevice( Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface );
}


Main.cpp(使用Microsoft Detours 3.0 ):

#include <detours.h>
#include "D3D.h"

typedef HMODULE (WINAPI * HookLoadLibraryA)( LPCSTR lpFileName );
typedef IDirect3D8 *(WINAPI * HookDirect3DCreate8)( UINT SdkVersion );
typedef HRESULT (WINAPI * HookCreateDevice8)( IDirect3DDevice8* pInterface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice8** ppReturnedDeviceInterface );
HookLoadLibraryA RealLoadLibraryA;
HookDirect3DCreate8 RealDirect3DCreate8;
HookCreateDevice8 RealCreateDevice8;
//...
CD3D<IDirect3D8> *m_d3d8;
CD3DDevice<IDirect3D8> *m_d3dDev8;
//...
RealLoadLibraryA = (HookLoadLibraryA)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)RealLoadLibraryA, FakeLoadLibraryA);
DetourTransactionCommit();
//...

VOID VirtualHook( PVOID pInterface, PVOID pHookProc, PVOID pOldProc, int iIndex )
{
    // Hook a procedure within an interface's virtual table
    PDWORD pVtable = (PDWORD)*((PDWORD)pInterface);
    DWORD lpflOldProtect;

    VirtualProtect( (PVOID)&pVtable[iIndex], sizeof(DWORD), PAGE_READWRITE, &lpflOldProtect );
    if( pOldProc ) *(DWORD*)pOldProc = pVtable[iIndex];
    pVtable[iIndex] = (DWORD)pHookProc;
    VirtualProtect( pVtable, sizeof(DWORD), lpflOldProtect, &lpflOldProtect );
}

HRESULT WINAPI FakeCreateDevice8( IDirect3DDevice8* pInterface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice8** ppReturnedDeviceInterface )
{
    HRESULT ret = RealCreateDevice8( pInterface, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface );
    // Save the registers
    __asm pushad
    if(*ppReturnedDeviceInterface != NULL)
        m_d3dDev8 = reinterpret_cast<CD3DDevice<IDirect3D8> *>(*ppReturnedDeviceInterface);
    // Restore the registers
    __asm popad
    return ret;
}

IDirect3D8 *WINAPI FakeDirect3DCreate8( UINT SdkVersion )
{
    m_d3d8 = reinterpret_cast<CD3D<IDirect3D8> *>(RealDirect3DCreate8( SdkVersion ));
    if( m_d3d8 ) {
        // Hook CreateDevice (vftable index #15)
        VirtualHook( m_d3d8, &FakeCreateDevice8, &RealCreateDevice8, 15 );
    }
    return m_d3d8;
}

HMODULE WINAPI FakeLoadLibraryA( LPCSTR lpFileName )
{
    CStringA strFileName( lpFileName );
    int i = strFileName.ReverseFind('\\');
    if(i != -1) strFileName = strFileName.Right(i + 1);
    if( strFileName.CompareNoCase("d3d8.dll") == 0 )
    {
        // Hook Direct3DCreate8
        HMODULE m_hD3D = RealLoadLibraryA( lpFileName );
        RealDirect3DCreate8 = (HookDirect3DCreate8)GetProcAddress(m_hD3D, "Direct3DCreate8");
        DetourTransactionBegin();
        DetourUpdateThread( GetCurrentThread() );
        DetourAttach(&(PVOID&)RealDirect3DCreate8, FakeDirect3DCreate8);
        DetourTransactionCommit();
        return m_hD3D;
    }
}



...鈎子函數被調用,但是我的包裝函數都沒有被調用,並且注入的應用程序正常運行。 為什么是這樣? 當然,我可以為每個DirectX界面(針對Direct3D的每個版本)中找到的每個函數手動設置掛鈎,但是這樣做的目的是試圖避免這樣做,並使其更加整潔。 那么有沒有辦法使它按我的預期工作? 謝謝!

您正在使用reinterpret_cast ,它不會做很多事情,因為基礎對象仍然具有指向“真實” IDirect3D8IDirect3DDevice8 COM vtable的指針,這些指針將用於調用。

而是通過CD3DDevice傳遞原始對象實例來實例化自定義CD3DCD3DDevice 然后修改所有調用,以在原始對象上回調正確的方法-您的類有效地充當透明代理。

我的意思是這樣的:

STDIMP8_(ULONG, AddRef, THIS) {
    return realObject->AddRef();
}

realObject是原始IDirect3D8*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM