Greetings,
I'm trying to create an overlay of text onto a game and having very little luck. I'm using a DLL with an injector to get access to the game and to put my own code in. It works via Function detours and has been very nice to me up to this point.
Before I get into my code, let me explain what happens when DrawText() is called. When the game starts up, it has a logo screen and a login screen. At this point, the text is draw successfully into the game. After that, the game loads a map and that's where I have a problem. It crashes with an error of:
Quote:
The instruction at "0x00000904" referenced memory at "0x00000904". The memory could not be "read".
I've tried using two methods for creating the font, but that isn't the problem. The font itself stays intact (or atleast there are no errors). The code I have is:
//In d3ddev.cpp
#include <stdio.h>
ID3DXFont *m_pFont = NULL;
//In BeginScene():
if (!bCreatedFont)
{
LOGFONT log_font={
32, //height
0, //width;
0, // lfEscapement;
0, //lfOrientation;
FW_BOLD, // lfWeight;
FALSE, // lfItalic;
FALSE, // lfUnderline;
FALSE, // lfStrikeOut;
DEFAULT_CHARSET, // lfCharSet;
OUT_DEFAULT_PRECIS, //lfOutPrecision;
CLIP_DEFAULT_PRECIS, // lfClipPrecision;
ANTIALIASED_QUALITY,// lfQuality;
DEFAULT_PITCH,// lfPitchAndFamily;
"Arial"// lfFaceName[LF_FACESIZE];
};
HRESULT hr = D3DXCreateFontIndirect(m_pD3Ddev,&log_font,&m_pFont);
if(FAILED(hr)) add_log("Error creating font");
bCreatedFont = true;
}
//At the end of the scene ( EndScene() ):
if(bDrawText && m_pFont)
{
PrintTextB( m_pFont,
250, 20, //x, y
255, 255, 255, 255, //color values (and then alpha)
"Testing 1234567890!!!");
}
//My custom PrintText Function:
void PrintTextB(ID3DXFont *Font, int x, int y, int Red, int Green, int Blue, int Alpha, const char *text, ...)
{
D3DCOLOR fontColor = D3DCOLOR_ARGB(Alpha, Red, Green, Blue);
RECT rct;
rct.left=x; rct.top=y; rct.right=rct.left+1000; rct.bottom=rct.top+1000;
va_list va_alist;
char logbuf[256] = {0};
va_start (va_alist, text);
_vsnprintf (logbuf+strlen(logbuf), sizeof(logbuf) - strlen(logbuf), text, va_alist);
va_end (va_alist);
if (Font && logbuf && &rct && fontColor)
{
Font->Begin();
//It crashes here at DrawText()
Font->DrawText(logbuf, -1, &rct, 0, fontColor);
Font->End();
}
}
//Finally I Release m_pFont on DLL detach (exit):
bool WINAPI DllMain(HMODULE hDll, DWORD dwReason, PVOID pvReserved)
{
if(dwReason == DLL_PROCESS_ATTACH)
{
//Code snipped for brevity
}
else if(dwReason == DLL_PROCESS_DETACH)
{
if(ofile) { ofile.close(); } //Close the logfile
if (m_pFont) {m_pFont->Release();m_pFont=NULL;} //Release font.
}
return false;
}
I'm using DrunkenHyena's examples from his wonderful webpage and I've made sure I did everything he explained. So I'm not quite understanding why it crashes. I've been through the MSDN SDK references and SDK manuals and from what I can tell, my code should work flawlessly. If anyone could help point me in the right direction, I will be forever grateful.
Thanx in advance,
-CMR