Solved DirectX How to Draw Menu Text?

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Chriko2502

Newbie
Full Member
Jul 18, 2013
8
172
0
Hi,

following code will not work :(
When i comment the "drawing text things" out it will work fine...

So perhaps u can tell me why i get the following error:
C++:
Quelle.obj : error LNK2001: Nicht aufgelöstes externes Symbol "_D3DXCreateFontA@48".
Thanks for helping! :)

C++:
#include <Windows.h> 
#include <cstdio> 
#include <time.h>
#include <iostream>
#include <d3d9.h> 
#include <d3dx9.h>  

const D3DCOLOR fontRed = D3DCOLOR_ARGB(255, 255, 0, 0);
const D3DCOLOR fontGreen = D3DCOLOR_ARGB(255, 0, 255, 0);
const D3DCOLOR fontBlue = D3DCOLOR_ARGB(255, 0, 0, 255);
const D3DCOLOR fontWhite = D3DCOLOR_ARGB(255, 255, 255, 255);
const D3DCOLOR fontBlack = D3DCOLOR_ARGB(255, 0, 0, 0);

HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice); 

typedef HRESULT(__stdcall* EndScene_t)(LPDIRECT3DDEVICE9); 
EndScene_t pEndScene;  

DWORD WINAPI HookThread();
void CreateLog(char* format, ...); 
void* DetourFunc(PBYTE src, const PBYTE dst, const int len);
bool bDataCompare(const BYTE* pData, const BYTE* bMask, const char* szMask);
DWORD dwFindPattern(DWORD dwAddress,DWORD dwLen,BYTE *bMask,char * szMask);  

void DrawFilledRectangle(int x, int y, int w, int h, D3DCOLOR color, IDirect3DDevice9 *d3dDevice);
void DrawBorderBox(int x, int y, int w, int h, int thickness, D3DCOLOR color, IDirect3DDevice9 *d3dDevice);
void DrawFont (int X, int Y, D3DCOLOR Color, char *format, ...);

HMODULE hModD3D9 = NULL; 
FARPROC dwEndScene = NULL;
HANDLE tmpHandle = NULL;
DWORD* VTableStart = NULL;
DWORD tempadd = NULL;

ID3DXFont *pFont = NULL;

BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD Reason,LPVOID Reserved) {  
	switch(Reason) {  
	case DLL_PROCESS_ATTACH:    
		CreateLog("==========LOG START=========="); 
		CreateLog("DLL Attached");    
		CreateLog("Creating Thread...");    
		tmpHandle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&HookThread, 0, 0, 0);    
		if (!tmpHandle) {    
			CreateLog("ThreadCreation Failed!"); 
		}    break;  
	case DLL_PROCESS_DETACH:   
		CreateLog("DLL Detached");  
		CreateLog("==========LOG END==========\n\n\n");  
		break;  
	} 
	return 1;
}  

DWORD WINAPI HookThread(void) {
	CreateLog("Thread Created");  
	
	while (!hModD3D9) {   
		CreateLog("Searching d3d9.dll..."); 
		hModD3D9 = GetModuleHandle("d3d9.dll"); 
		Sleep(100);  
	}  
	CreateLog("Found d3d9.dll: %x !", hModD3D9);  

	tempadd = dwFindPattern((DWORD)hModD3D9, 0x128000, (PBYTE)"\xC7\x06\x00\x00\x00\x00\x89\x86\x00\x00\x00\x00\x89\x86", "xx????xx????xx");
	CreateLog("PatternFound!"); 
	VTableStart = (DWORD*) *(DWORD*)(tempadd+2);  

	dwEndScene = (FARPROC) VTableStart[42]; 
	pEndScene = (EndScene_t) DetourFunc((PBYTE) dwEndScene, (PBYTE)hkEndScene, 5); 

	while (true)  { 
		Sleep(500); 
	} 
	return 0;
}  

void CreateLog(char* format, ...) { 
	HANDLE filehandle; 
	DWORD dwReadBytes;  
	char buffer[2048]; 
	char writebuffer[2048]; 
	va_list args;
	va_start(args, format);
	vsprintf (buffer, format, args);  
	filehandle = CreateFile("Log.txt", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0); 
	SetFilePointer(filehandle, 0, 0, FILE_END); 
	char date[18]; 
	_strdate(date); 
	date[8] = ' ';  
	_strtime(date+9);
	sprintf_s(writebuffer, 2048, "Log Added (%s): %s\r\n", date, buffer);
	WriteFile(filehandle, writebuffer, strlen(writebuffer), &dwReadBytes, 0);  
	CloseHandle(filehandle);
} 

 void* DetourFunc(PBYTE src, const PBYTE dst, const int len) { 
	 DWORD dwback; 
	 BYTE* jmp = (BYTE*)malloc(len+5); 
	 VirtualProtect(src, len, PAGE_READWRITE, &dwback); 
	 memcpy(jmp, src, len); 
	 jmp += len;  
	 jmp[0] = 0xE9; 
	 *(DWORD*)(jmp+1) = (DWORD)(src + len - jmp) - 5; 
	 src[0] = 0xE9;
	 *(DWORD*)(src+1) = (DWORD)(dst - src) - 5; 
	 VirtualProtect(src, len, dwback, &dwback); 
	 VirtualProtect(jmp-len, len+5, PAGE_EXECUTE_READWRITE, &dwback); 
	 return (jmp - len); 
 } 

bool bDataCompare(const BYTE* pData, const BYTE* bMask, const char* szMask) { 
	for(;*szMask;++szMask,++pData,++bMask)  { 
		if(*szMask=='x' && *pData!=*bMask )   {  
			return false;  
		}  
	} 
	return (*szMask) == NULL; 
}  

DWORD dwFindPattern(DWORD dwAddress,DWORD dwLen,BYTE *bMask,char * szMask) {  
	for(DWORD i=0; i < dwLen; i++)  {  
		if( bDataCompare( (BYTE*)( dwAddress+i ),bMask,szMask) )   { 
			return (DWORD)(dwAddress+i);   
		}  
	} 
	return 0; 
}  

HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice) { 
	CreateLog("DrawIngameMenu"); 
	
	DrawFilledRectangle(55, 20, 200, 50, fontBlue, pDevice);
	DrawBorderBox(55, 20, 200, 50, 4, fontBlack, pDevice);

	D3DXCreateFont(pDevice, 14, 0, FW_NORMAL, 1, 0, DEFAULT_CHARSET,  OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &pFont );      
	DrawFont( 300, 50, fontRed, "My first Text");


	return pEndScene(pDevice); 
} 

void DrawFilledRectangle(int x, int y, int w, int h, D3DCOLOR color, IDirect3DDevice9 *d3dDevice) {
	CreateLog("DrawFilledRectangle"); 
	D3DRECT rct = {x , y, x + w, y + h};
	d3dDevice->Clear(1, &rct, D3DCLEAR_TARGET | D3DCLEAR_TARGET, color, 0, 0);
}

void DrawBorderBox(int x, int y, int w, int h, int thickness, D3DCOLOR color, IDirect3DDevice9 *d3dDevice){
	CreateLog("DrawBorderBox");
	DrawFilledRectangle(x, y, w, thickness, color, d3dDevice);
	DrawFilledRectangle(x, y, thickness, h, color, d3dDevice);
	DrawFilledRectangle(x + w, y, thickness, h, color, d3dDevice);
	DrawFilledRectangle(x, y+h, thickness + w, thickness, color, d3dDevice);
}


void DrawFont (int X, int Y, D3DCOLOR Color, char *format, ...) { 
	char buffer[256];        
	va_list args; // deswegen: #include <cstdio>      
	va_start (args, format);   
	vsprintf (buffer,format, args);    
	RECT FontRect = { X, Y, X + 120, Y + 16 };    
	pFont->DrawText( NULL, buffer, -1, &FontRect, DT_NOCLIP , Color ); // Zeichnen  
	va_end (args); 
}
 

Chriko2502

Newbie
Full Member
Jul 18, 2013
8
172
0
i included the sdk/ d3d9.lib / d3dx9.lib... :(

"why are you creating a font each frame?"
yeah sure in final version i wont do that, this was only a test and he failed :(

Some help left for me?
 

till0sch

Respected Hacker
Dank Tier VIP
Dank Tier Donator
Oct 14, 2012
1,104
12,593
51
Just check what Fleep did on his d3d menu tutorial, how he included the SDK etc. You propably just did sth wrong.
 

Icyvel

Newbie
Full Member
Aug 6, 2013
17
198
0
i included the sdk/ d3d9.lib / d3dx9.lib... :(

"why are you creating a font each frame?"
yeah sure in final version i wont do that, this was only a test and he failed :(

Some help left for me?
Just do a static int DoOnce if statement, this is actually crucial, i did the same mistake in CSS through the engine and i ended up reaching maximum memory the game would allow pretty quickly, seeing as the standard heap-size is not that big.

It's actually a memory leak, and you'll end up noticing it pretty quickly if you watch over your memory with the TaskManager or similar :D.

Example::

static int DoOnce = true;

if(DoOnce)
{

CreateFont....

DoOnce = false;

}
 

Amphetamin

Newbie
Full Member
Aug 19, 2013
5
192
0
i included the sdk/ d3d9.lib / d3dx9.lib... :(

"why are you creating a font each frame?"
yeah sure in final version i wont do that, this was only a test and he failed :(

Some help left for me?
I create my Font in the StartUp function...
C++:
HRESULT CDirectX::StartUp( HWND hWnd )
{
		BOOL		bComposition			= false;
		DWORD		msqAAQuality			= 0;
		HRESULT		hResult;
	
		DwmIsCompositionEnabled( &bComposition );
		if( !bComposition )
			return E_FAIL;
	
		hResult = Direct3DCreate9Ex( D3D_SDK_VERSION, &Handle.p_D3D );
		if( FAILED( hResult ) )
			return E_FAIL;
			
		ZeroMemory( &Handle.p_Parameter, sizeof( Handle.p_Parameter ) );
		Handle.p_Parameter.Windowed					= TRUE;
		Handle.p_Parameter.SwapEffect				= D3DSWAPEFFECT_DISCARD;
		Handle.p_Parameter.BackBufferFormat			= D3DFMT_A8R8G8B8;
		Handle.p_Parameter.EnableAutoDepthStencil	= TRUE;
		Handle.p_Parameter.AutoDepthStencilFormat	= D3DFMT_D16;
		Handle.p_Parameter.MultiSampleType			= D3DMULTISAMPLE_NONE;
		Handle.p_Parameter.PresentationInterval		= 0x80000000L;
	
		if( SUCCEEDED( Handle.p_D3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8,
														  true, D3DMULTISAMPLE_NONMASKABLE, &msqAAQuality ) ) )
		{
			Handle.p_Parameter.MultiSampleType		= D3DMULTISAMPLE_NONMASKABLE;
			Handle.p_Parameter.MultiSampleQuality	= msqAAQuality -1;
		}
		else
		{
			Handle.p_Parameter.MultiSampleType		= D3DMULTISAMPLE_NONE;
		}
	
		hResult = Handle.p_D3D->CreateDeviceEx( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &Handle.p_Parameter, NULL, &Handle.p_Device );
		if( FAILED( hResult ) )
			return E_FAIL;
	
		if( FAILED( D3DXCreateFontA( Handle.p_Device, 14, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
									 DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial", &Handle.p_Font ) ) )
			return E_FAIL;		

		return S_OK;
}
Example::
static int DoOnce = true;
Hm.. don't use an Integer for true/false check.. It works but it isnt correct
 
Last edited:

Icyvel

Newbie
Full Member
Aug 6, 2013
17
198
0
Hm.. don't use an Integer for true/false check.. It works but it isnt correct
It's as correct as anything else, TRUE & FALSE are just keywords to make it easier to understand programming, false is in fact equal to 0 in numeric value, and TRUE is 1 or anything positive. So each time you're using true / false you're comparing to 0 or a positive number, in which case here I'm actually assigning the value 1 to DoOnce, and the if statement checks if the DoOnce is 1 (not executed code yet) or 0 (it already executed the code).
 

Amphetamin

Newbie
Full Member
Aug 19, 2013
5
192
0
It's as correct as anything else, TRUE & FALSE are just keywords to make it easier to understand programming, false is in fact equal to 0 in numeric value, and TRUE is 1 or anything positive. So each time you're using true / false you're comparing to 0 or a positive number, in which case here I'm actually assigning the value 1 to DoOnce, and the if statement checks if the DoOnce is 1 (not executed code yet) or 0 (it already executed the code).
I know that false is equal 0 / true equal is 1 in numeric value. Of course it works, but it's not the right way.
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods