~ruther/NosSmooth

ref: ccdf2299f74571291fdc4fd5df5830b2ce085e6d NosSmooth/Local/NosSmooth.LocalCore/ModuleHook.cpp -rw-r--r-- 1.3 KiB
ccdf2299 — František Boháček feat: add simple chat sample 3 years ago
                                                                                
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
#include "ModuleHook.h"
#include <psapi.h>
using namespace NosSmoothCore;

ModuleHook ModuleHook::CreateNostaleXDatModule()
{
	auto moduleHandle = GetModuleHandleA(nullptr);
	if (moduleHandle == nullptr)
	{
		throw "Could not obtain NostaleX.dat module handle";
	}

	MODULEINFO moduleInfo = {};
	if (!GetModuleInformation(GetCurrentProcess(), moduleHandle, &moduleInfo, sizeof(moduleInfo)))
	{
		throw "Could not get module handle information";
	}
	unsigned int moduleBase = reinterpret_cast<unsigned int>(moduleInfo.lpBaseOfDll);
	unsigned int moduleSize = moduleInfo.SizeOfImage;

	return ModuleHook(moduleBase, moduleSize);
}

ModuleHook::ModuleHook(unsigned int baseAddress, unsigned int moduleSize)
	: _baseAddress(baseAddress), _moduleSize(moduleSize)
{
}

unsigned int ModuleHook::FindPattern(const BYTE* lpPattern, LPCSTR szMask)
{
	DWORD dwLength = strlen(szMask);
	DWORD dwImageEnd = _baseAddress + _moduleSize - dwLength;
	DWORD_PTR i, j;

	// Scan the whole image for the pattern
	for (j = _baseAddress; j < dwImageEnd; ++j)
	{
		// If the pattern is found, return the address at which it begins
		for (i = 0; i < dwLength && (szMask[i] == '?' || *(BYTE*)(j + i) == lpPattern[i]); ++i);
		if (i == dwLength) return j;
	}

	return NULL;
}

unsigned int ModuleHook::GetModuleBaseAddress()
{
	return _baseAddress;
}
Do not follow this link