Jump to content

  • Log In with Google      Sign In   
  • Create Account


Locating address of IDirect3DTexture9 in memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Naruto-kun   Members   -  Reputation: 333

Like
0Likes
Like

Posted 15 October 2012 - 11:02 AM

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 offsetg_PatchCode[0] = 0xE9;*((DWORD*) &g_PatchCode[1]) = offset;g_pPatchAddr = (unsigned char*)RealPresent;//Set permission to allow reading/write/executeVirtualProtect(g_pPatchAddr, 8, PAGE_EXECUTE_READWRITE, &dwOldProtect);//Save out the original bytesfor (int i = 0; i < 5; i++) g_OrigCode[i] = *(g_pPatchAddr + i);//Copy in our patch codeApplyPatch();//Delete dummy D3D objectspI->Release();p->Release();return;}//Our hooked function. FSX calls this thinking it's calling IDirect3DDevice9::PresentHRESULT _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 insteadvoid 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 codevoid 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, 15 October 2012 - 11:07 AM.


Sponsor:

#2 kubera   Members   -  Reputation: 906

Like
0Likes
Like

Posted 16 October 2012 - 04:22 AM

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.

#3 Naruto-kun   Members   -  Reputation: 333

Like
0Likes
Like

Posted 16 October 2012 - 06:26 AM

The second thing, GDI and GDI+ are deprecated.


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

It looks like a kind of hacking.
In my opinion this would be not a good practice.


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, 16 October 2012 - 06:27 AM.


#4 kauna   Crossbones+   -  Reputation: 2363

Like
1Likes
Like

Posted 16 October 2012 - 07:34 AM

Hi,

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

Cheers!

#5 Naruto-kun   Members   -  Reputation: 333

Like
0Likes
Like

Posted 16 October 2012 - 11:24 PM

Thats what I would like to do....unsure how though.

#6 kauna   Crossbones+   -  Reputation: 2363

Like
0Likes
Like

Posted 17 October 2012 - 06:11 AM

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!

#7 Naruto-kun   Members   -  Reputation: 333

Like
0Likes
Like

Posted 18 October 2012 - 10:14 AM

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, 18 October 2012 - 10:14 AM.


#8 mhagain   Crossbones+   -  Reputation: 7867

Like
0Likes
Like

Posted 18 October 2012 - 12:05 PM

Well like kauna said, intercept the IDirect3DDevice9::SetTexture call then. You'll get the address of the texture being set right from it.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS