forked from bb107/MemoryModulePP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.cpp
More file actions
51 lines (44 loc) · 1.49 KB
/
load.cpp
File metadata and controls
51 lines (44 loc) · 1.49 KB
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
#include "../MemoryModule/stdafx.h"
#include <cstdio>
static PVOID ReadDllFile(LPCSTR FileName) {
LPVOID buffer;
size_t size;
FILE* f;
fopen_s(&f, FileName, "rb");
if (!f)return 0;
_fseeki64(f, 0, SEEK_END);
if (!(size = _ftelli64(f))) {
fclose(f);
return 0;
}
_fseeki64(f, 0, SEEK_SET);
fread(buffer = new char[size], 1, size, f);
fclose(f);
return buffer;
}
int __stdcall test_user32() {
HMODULE hModule;
NTSTATUS status;
PVOID buffer = ReadDllFile("C:\\Windows\\System32\\user32.dll");
if (!buffer) return 0;
hModule = GetModuleHandleA("user32.dll");
if (hModule)return 0;
status = LdrLoadDllMemoryExW(
&hModule, // ModuleHandle
nullptr, // LdrEntry
0, // Flags
buffer, // Buffer
0, // Reserved
L"user32.dll", // DllBaseName
L"C:\\Windows\\System32\\user32.dll" // DllFullName
);
if (NT_SUCCESS(status) && status != STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
auto _MessageBoxW = (decltype(&MessageBoxW))GetProcAddress(hModule, "MessageBoxW");
_MessageBoxW(nullptr, L"Hello, from memory user32!", L"Caption", MB_OK);
//
// After calling MessageBox, we can't free it.
//
//LdrUnloadDllMemory(hModule);
}
return 0;
}