Advertisement Jump to content
Sign in to follow this  
CodeMaster Rapture

[DirectX 8] Having a heck of a time with DrawText()

This topic is 4906 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

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:
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; 
			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.right=rct.left+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)
		//It crashes here at DrawText()
 		Font->DrawText(logbuf, -1, &rct, 0, fontColor);

//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

Share this post

Link to post
Share on other sites
I see a potential error here:

if(FAILED(hr)) add_log("Error creating font");
bCreatedFont = true;

Shouldn't that be:

add_log("Error creating font");
return; // Failure, nothing left to do
bCreatedFont = true;

Or use an 'else' clause.

Now, I can't help you much myself from looking at the code, since it all looks like it should work. However, have you tried running your application windowed and getting a stacktrace on the point of the crash?


Share this post

Link to post
Share on other sites
I'm not exactly too sure what a stack trace is, but I get this from OllyDBG on the crash:


Stack [0012F828]=77C114BC (msvcrt.77C114BC), ASCII "Access violation - no RTTI data!"

I also have no idea what that means, but I'll google a little and see what I can find.

Thanx for the suggestion,

According to this website ( the problem was a project setting. So I changed it how it suggested and I still get the error.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!