• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Naruto-kun

Locating address of IDirect3DTexture9 in memory

7 posts in this topic

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
0

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.
0

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
0

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!
1

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!
0

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
0

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0