-
Notifications
You must be signed in to change notification settings - Fork 4
/
Common.cpp
72 lines (57 loc) · 1.89 KB
/
Common.cpp
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
#pragma once
#include "Common.h"
void Common::OpenConsole()
{
AllocConsole();
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(console, &lpConsoleScreenBufferInfo);
SetConsoleScreenBufferSize(console, { lpConsoleScreenBufferInfo.dwSize.X, 30000 });
}
void Common::CloseConsole()
{
FreeConsole();
}
// loosely based on https://stackoverflow.com/q/26572459
BYTE* Common::GetModuleBaseAddress(wchar_t* moduleName)
{
auto pid = GetCurrentProcessId();
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
BYTE* baseAddress = nullptr;
if (INVALID_HANDLE_VALUE != snapshot)
{
MODULEENTRY32 moduleEntry = { 0 };
moduleEntry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(snapshot, &moduleEntry))
{
do
{
// Check that module entry name *starts* with our key...
if (moduleEntry.szModule == wcsstr(moduleEntry.szModule, moduleName))
{
baseAddress = moduleEntry.modBaseAddr;
break;
}
} while (Module32Next(snapshot, &moduleEntry));
}
CloseHandle(snapshot);
}
return baseAddress;
}
BYTE* Common::GetFirstModuleBaseAddress()
{
DWORD const pid = GetCurrentProcessId();
HANDLE const snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
BYTE* baseAddress = nullptr;
if (INVALID_HANDLE_VALUE != snapshot)
{
MODULEENTRY32 moduleEntry = { 0 };
moduleEntry.dwSize = sizeof(MODULEENTRY32);
Module32First(snapshot, &moduleEntry);
baseAddress = moduleEntry.modBaseAddr;
CloseHandle(snapshot);
}
return baseAddress;
}