When Will I Reach This Level Of Coding?

Hi, I've been learning C++ for about a month now, and I was feeling pretty good that I can look at code and understand it - until I looked at a game hack someone coded and couldn't understand a thing. Now, this ISN'T about hacking - I was just looking at code. I've put the code to be reviewed below.

Now, I understand that the reason I don't know any of this code is because it's using DirectX-SDK and "detours" kind of functions. Now, these functions are used while coding with C++ - But are these functions TRULY a part of C++? Or do DirectX and such provide the needed library? Moreover, it would be appreciated if someone could point me to a tutorial that covers this kind of coding - so I know where to go next after finishing my current C++ tutorial. Thanks !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "windows.h"
#include "intrin.h"
#include "d3d9.h"
#include "d3dx9.h"
#include "Detours.h"

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "Detours.lib")

#pragma intrinsic(_ReturnAddress)

#define Player 0x68EBA1
bool bInit = false;
bool bChams = true;
LPDIRECT3DTEXTURE9 texRed = NULL;

typedef HRESULT(WINAPI *Prototype_Present)(LPDIRECT3DDEVICE9, CONST RECT*, CONST RECT*, HWND, CONST RGNDATA*);
typedef HRESULT(WINAPI *Prototype_DrawIndexedPrimitive)(LPDIRECT3DDEVICE9, D3DPRIMITIVETYPE, INT, UINT, UINT, UINT, UINT);

Prototype_Present Original_Present;
Prototype_DrawIndexedPrimitive Original_DrawIndexedPrimitive;

HRESULT WINAPI Hooked_Present(LPDIRECT3DDEVICE9 Device, CONST RECT *pSrcRect, CONST RECT *pDestRect, HWND hDestWindow, CONST RGNDATA *pDirtyRegion);
HRESULT WINAPI Hooked_DrawIndexedPrimitive(LPDIRECT3DDEVICE9 Device, D3DPRIMITIVETYPE PrimType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount);

DWORD FindDevice(DWORD Len)
{
	DWORD dwObjBase = 0;

	dwObjBase = (DWORD)LoadLibrary("d3d9.dll");
	while (dwObjBase++ < dwObjBase + Len)
	{
		if ((*(WORD*)(dwObjBase + 0x00)) == 0x06C7 && (*(WORD*)(dwObjBase + 0x06)) == 0x8689 && (*(WORD*)(dwObjBase + 0x0C)) == 0x8689) {
			dwObjBase += 2; break;
		}
	}
	return(dwObjBase);
}

DWORD GetDeviceAddress(int VTableIndex)
{
	PDWORD VTable;
	*(DWORD*)&VTable = *(DWORD*)FindDevice(0x128000);
	return VTable[VTableIndex];
}

HRESULT GenerateTexture(LPDIRECT3DDEVICE9 pDevice, IDirect3DTexture9 **ppD3Dtex, DWORD colour32)
{
	if (FAILED(pDevice->CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, NULL)))
		return E_FAIL;

	WORD colour16 = ((WORD)((colour32 >> 28) & 0xF) << 12)
		| (WORD)(((colour32 >> 20) & 0xF) << 8)
		| (WORD)(((colour32 >> 12) & 0xF) << 4)
		| (WORD)(((colour32 >> 4) & 0xF) << 0);

	D3DLOCKED_RECT d3dlr;
	(*ppD3Dtex)->LockRect(0, &d3dlr, 0, 0);
	WORD *pDst16 = (WORD*)d3dlr.pBits;

	for (int xy = 0; xy < 8 * 8; xy++)
		*pDst16++ = colour16;

	(*ppD3Dtex)->UnlockRect(0);

	return S_OK;
}

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
		if (!bInit) {
			Original_Present = (Prototype_Present)DetourFunction((PBYTE)GetDeviceAddress(17), (PBYTE)Hooked_Present);
			Original_DrawIndexedPrimitive = (Prototype_DrawIndexedPrimitive)DetourFunction((PBYTE)GetDeviceAddress(82), (PBYTE)Hooked_DrawIndexedPrimitive);
			bInit = true;
		}
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		DetourRemove((PBYTE)Original_Present, (PBYTE)Hooked_Present);
		DetourRemove((PBYTE)Original_DrawIndexedPrimitive, (PBYTE)Hooked_DrawIndexedPrimitive);
	}

	return TRUE;
}

HRESULT WINAPI Hooked_Present(LPDIRECT3DDEVICE9 Device, CONST RECT *pSrcRect, CONST RECT *pDestRect, HWND hDestWindow, CONST RGNDATA *pDirtyRegion) {

	if (texRed == NULL) {
		GenerateTexture(Device, &texRed, D3DCOLOR_ARGB(255, 255, 0, 0));
	}

	return Original_Present(Device, pSrcRect, pDestRect, hDestWindow, pDirtyRegion);
}

HRESULT WINAPI Hooked_DrawIndexedPrimitive(LPDIRECT3DDEVICE9 Device, D3DPRIMITIVETYPE PrimType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {

	void* ReturnAddress = _ReturnAddress();

	if (ReturnAddress == (void*)Player && bChams) {
		Device->SetRenderState(D3DRS_ZENABLE, FALSE);
		Device->SetTexture(0, texRed);
		Original_DrawIndexedPrimitive(Device, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
		Device->SetRenderState(D3DRS_ZENABLE, TRUE);
		Device->SetTexture(0, texRed);
	}

	return Original_DrawIndexedPrimitive(Device, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
}
Last edited on
But are these functions TRULY a part of C++? Or do DirectX and such provide the needed library?
No, DirectX and Detours are third party libraries.

It's worth noting that even if you were a C++ guru, there could still be pure C++ programs (as in, programs that use only C++ features) that you may not completely understand, for exactly the same reason that just because you understand English doesn't mean that you can understand, say, a book about fluid dynamics.
Thanks for the informative reply helios! However, are you saying that even if I were to learn DirectX and Detours - this may still be code I wouldn't understand? And if so, that would mean I lack knowledge of a specific concept rather than lack of knowledge in programming. What exactly should I be learning on top of C++ or those other libraries to be able to understand such code?

And quick curious question - But can you understand the code above?
However, are you saying that even if I were to learn DirectX and Detours - this may still be code I wouldn't understand?
No, that's not what I said. This is one specific program. To understand a specific program you don't need to completely understand all the libraries it uses, just like how to understand one sentence you don't need to understand the language it is in.
But to understand every possible sentence in a language you need to at least understand the whole language, and probably things other than the language.

What exactly should I be learning on top of C++ or those other libraries to be able to understand such code?
If you want to understand code like this you should start by learning C++, and then graphics programming, and hooking.

And quick curious question - But can you understand the code above?
Yes, I can roughly understand the intent even though I've never used DirectX or Detours, but I have experience with hooking from my job, which is somewhat atypical, and with graphics programming.
Alright, Thanks again helios. This is one of those things where the more you learn the more you realize you don't know! As soon as I'm finished with C++, I'll dive into graphics programming and hooking. Already bookmarked some tutorials that looked useful.

Sometimes I wish I could copy people's knowledge and skills to my own. Anyway, Thanks again helios - don't know what I'd do without you.
Topic archived. No new replies allowed.