I do not use SlimDX, but you want something like this. Just send me a PM if you want the full set of classes for my generic hook x32/64 d3d9/11 hook that checks which one to detour at runtime.
Code:
[DllImport("d3d11.dll")]
public static extern unsafe int D3D11CreateDeviceAndSwapChain(void* pAdapter, int driverType, void* Software,
int flags, void* pFeatureLevels,
int FeatureLevels, int SDKVersion,
void* pSwapChainDesc, void* ppSwapChain,
void* ppDevice, void* pFeatureLevel,
void* ppImmediateContext);
Then you grab the device pointer
Code:
protected override void InitD3D(out IntPtr d3DDevicePtr)
{
LoadDxgiDll();
var scd = new SwapChainDescription
{
BufferCount = 1,
ModeDescription = new ModeDescription {Format = DXGI_FORMAT_R8G8B8A8_UNORM},
Usage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
OutputHandle = Form.Handle,
SampleDescription = new SampleDescription {Count = 1},
IsWindowed = true
};
unsafe
{
var pSwapChain = IntPtr.Zero;
var pDevice = IntPtr.Zero;
var pImmediateContext = IntPtr.Zero;
var ret = D3D11CreateDeviceAndSwapChain((void*) IntPtr.Zero, D3D_DRIVER_TYPE_HARDWARE,
(void*) IntPtr.Zero, 0, (void*) IntPtr.Zero, 0, D3D11_SDK_VERSION, &scd, &pSwapChain, &pDevice,
(void*) IntPtr.Zero, &pImmediateContext);
_swapChain = pSwapChain;
_device = pDevice;
d3DDevicePtr = pImmediateContext;
if (ret >= 0)
{
var vTableFuncAddress = GetVTableFuncAddress(_swapChain, VTableIndexes.DXGISwapChainRelease);
_swapchainRelease = Marshal.GetDelegateForFunctionPointer<VTableFuncDelegate>(vTableFuncAddress);
var deviceptr = GetVTableFuncAddress(_device, VTableIndexes.D3D11DeviceRelease);
_deviceRelease = Marshal.GetDelegateForFunctionPointer<VTableFuncDelegate>(deviceptr);
var contex = GetVTableFuncAddress(d3DDevicePtr, VTableIndexes.D3D11DeviceContextRelease);
_deviceContextRelease = Marshal.GetDelegateForFunctionPointer<VTableFuncDelegate>(contex);
}
}
}
i personally use a generic hook that determines the d3d device when loaded, and applies the correct detour. Make sure to use IntPtr.Size for your vtables as to ensure x32/62 compatibly.
I think yours would look something like this
Code:
public class D3D11Hook : D3DHook
{
private DxgiSwapChainPresentDelegate _presentCallBack;
private Detour _presentDetour;
protected override void OnInitialize()
{
_presentCallBack = Dirext3D.HookAddress.ToDelegate<DxgiSwapChainPresentDelegate>();
_presentDetour = Detour("Present", _presentCallBack,
new DxgiSwapChainPresentDelegate(OnPresent), false);
base.OnInitialize();
}
private int OnPresent(IntPtr device, int syncInterval, int flags)
{
UpdateFrame();
return (int) _presentDetour.CallOriginal(device, syncInterval, flags);
}
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
protected delegate int DxgiSwapChainPresentDelegate(IntPtr swapChainPtr, int syncInterval, int flags);
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
protected delegate int DxgiSwapChainResizeTargetDelegate(
IntPtr swapChainPtr, ref D3D11Device.ModeDescription newTargetParameters);
}