Skip to content

Instantly share code, notes, and snippets.

@gashtio
Created October 29, 2013 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gashtio/7214320 to your computer and use it in GitHub Desktop.
Save gashtio/7214320 to your computer and use it in GitHub Desktop.
Getting the offsets of the IUnknown virtual functions for ID3D11Device.
#include <iostream>
#include <d3d11.h>
#pragma comment(lib, "d3d11.lib")
int main()
{
D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 };
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
HRESULT hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
featureLevels,
sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION,
&device,
nullptr,
&context);
if (SUCCEEDED(hr))
{
if (device)
{
INT_PTR d3d11ModuleOffset = (INT_PTR)::GetModuleHandleA("d3d11.dll");
// Get the virtual table pointer for ID3D11Device instances
INT_PTR* vtable = *(INT_PTR**)device;
// Print out the relative offsets for QueryInterface/AddRef/Release
for (int i = 0; i < 3; ++i)
{
std::cout.width(sizeof(INT_PTR) * 2);
std::cout.fill('0');
std::cout << std::hex << vtable[i] - d3d11ModuleOffset << std::endl;
}
device->Release();
}
if (context)
{
context->Release();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment