Sign in to follow this  

Locating address of IDirect3DTexture9 in memory

This topic is 1917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys

Im working on a project on a existing flightsimulator where i need to find the textures that are mapped to a particular model, and do my own rendering on them. The idea is to replace the glass instrument drawing system provided by the SDK as it is too framerate intensive for some applications (it uses GDI/GDI+).

I successfully hooked into the IDirect3DDevice9 instance using this code:

[source lang="cpp"] void HookIntoD3D()
{
//Create a temporary direct 3D object and get its IDirect3DDevice9 interface. This is a different
//"instance," but the virtual table will point to the function within d3d9.dll also used by FSX's
//instance.
LPDIRECT3D9 p = Direct3DCreate9(D3D_SDK_VERSION);
if (!p)
return;
D3DPRESENT_PARAMETERS presParams;
ZeroMemory(&presParams,sizeof(presParams));
presParams.Windowed = TRUE;
presParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
IDirect3DDevice9 *pI = NULL;
HRESULT hr;
hr = p->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, g_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &presParams, &pI);
if (!pI)
{
p->Release();
return;
}

//Get the pointer to the virtual table for IDirect3DDevice9 (pI is a pointer to a pointer to the Vtable)
PVOID* pVTable = (PVOID*)*((DWORD*)pI);

//Set permissions so we can read it (117 = size of whole table)
DWORD dwOldProtect;
VirtualProtect(pVTable, sizeof(void *) * 117, PAGE_READWRITE, &dwOldProtect);

//Get the address to the (real) Present function (17th function listed, starting at 0 -- see definition
//of IDirect3DDevice9).
RealPresent = (RealPresentFuncType)pVTable[17];

//Calculate the offset from the real function to our hooked function. Constant 5 is because JMP
//offset is from start of next instruction (5 bytes later)
DWORD offset =(DWORD)Present - (DWORD)RealPresent - 5;

//Create the patch code: JMP assembly instruction (0xE9) followed by relative offset
g_PatchCode[0] = 0xE9;
*((DWORD*) &g_PatchCode[1]) = offset;
g_pPatchAddr = (unsigned char*)RealPresent;

//Set permission to allow reading/write/execute
VirtualProtect(g_pPatchAddr, 8, PAGE_EXECUTE_READWRITE, &dwOldProtect);

//Save out the original bytes
for (int i = 0; i < 5; i++)
g_OrigCode[i] = *(g_pPatchAddr + i);

//Copy in our patch code
ApplyPatch();

//Delete dummy D3D objects
pI->Release();
p->Release();

return;
}

//Our hooked function. FSX calls this thinking it's calling IDirect3DDevice9::Present
HRESULT _stdcall Present(void *pThis, const RECT* pSourceRect,const RECT* pDestRect, HWND hDestWindowOverride, const RGNDATA* pDirtyRegion)
{
IDirect3DDevice9 *pI = (IDirect3DDevice9 *)pThis;

if(gD3D != pI)
{
gD3D = pI;
gD3D->CreateOffscreenPlainSurface(512, 400, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &gSurf, NULL);
}

//Call our main class
//g_GUI.OnFSXPresent(pI);

//Call the real "Present"
RemovePatch();
HRESULT hr = RealPresent(pThis, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
ApplyPatch();

return hr;
}

//Modify d3d9.dll Present function to call ours instead
void ApplyPatch()
{
for (int i = 0; i < 5; i++)
*(g_pPatchAddr + i) = g_PatchCode[i];

//FlushInstructionCache(GetCurrentProcess(), g_pPatchAddr, 5); //apparently not necessary on x86 and x64 CPU's?
return;
}

//Restore d3d9.dll's Present function to its original code
void RemovePatch()
{
for (int i = 0; i < 5; i++)
*(g_pPatchAddr + i) = g_OrigCode[i];

//FlushInstructionCache(GetCurrentProcess(), g_pPatchAddr, 5);
return;
} [/source]

My problem now is to find the relevant textures. There has to be a table or something somewhere....Can someone give me some clues on how to find it and what to look for?
Thanks
JB Edited by Naruto-kun

Share this post


Link to post
Share on other sites
It looks like a kind of hacking.
In my opinion this would be not a good practice.
The second thing, GDI and GDI+ are deprecated.
I am sorry, I do not know how to help you, but maybe you make a hard job without the future.

Share this post


Link to post
Share on other sites
[quote name='kubera' timestamp='1350382942' post='4990674']
The second thing, GDI and GDI+ are deprecated.
[/quote]

Precisely. Thats why im trying to find the texture so i can draw to it with directX.

[quote name='kubera' timestamp='1350382942' post='4990674']
It looks like a kind of hacking.
In my opinion this would be not a good practice.
[/quote]

Depends on the purpose. Not hacking really...a more politically correct term would be reverse engineering. The idea here however is not to get around copyrights and such but to enhance 3rd party addons created (by myself) for the sim. Edited by Naruto-kun

Share this post


Link to post
Share on other sites
Hi,

can't you hook to set texture calls / set pixel shader resources call to get a pointer to the texture object?

Cheers!

Share this post


Link to post
Share on other sites
Well, I'm not a master of dll injection or hooking but you seem to have some grasp how to intercept Present call for example. Isn't intercepting set texture call as easy?

Cheers!

Share this post


Link to post
Share on other sites
Im not a master either...this technique for hooking into the D3D device was provided by a friend of mine. Unfortunately he is clueless to what im after as well. Getting hold of a texture is a different story to intercepting a function call since a function call is being executed. So far in my tests with hooking its easier to track down a function that is being executed than to find a single variable or handle or such.... Edited by Naruto-kun

Share this post


Link to post
Share on other sites
Well like kauna said, intercept the IDirect3DDevice9::SetTexture call then. You'll get the address of the texture being set right from it.

Share this post


Link to post
Share on other sites
Sign in to follow this