forked from d00telemental/LENativeExperiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.h
67 lines (53 loc) · 1.71 KB
/
Common.h
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
#pragma once
#include <windows.h>
#include <TlHelp32.h>
#include <cstdio>
#include <string>
typedef unsigned char byte;
typedef signed long int32;
typedef unsigned long uint32;
typedef signed long long int64;
typedef unsigned long long uint64;
typedef wchar_t wchar;
namespace Common
{
void 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 CloseConsole()
{
FreeConsole();
}
// loosely based on https://stackoverflow.com/q/26572459
byte* GetModuleBaseAddress(wchar* 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
{
if (0 == wcscmp(moduleEntry.szModule, moduleName))
{
baseAddress = moduleEntry.modBaseAddr;
break;
}
} while (Module32Next(snapshot, &moduleEntry));
}
CloseHandle(snapshot);
}
return baseAddress;
}
}