forked from fancycode/MemoryModule
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
92 lines (75 loc) · 2.46 KB
/
test.cpp
File metadata and controls
92 lines (75 loc) · 2.46 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
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
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "../MemoryModule.hpp"
typedef BOOL (*HelloFunc)();
int main()
{
{
// Read the DLL from disk into a buffer (just for demo)
std::ifstream file("TestDll.dll", std::ios::binary);
std::vector<char> buffer((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
if (buffer.empty())
{
std::cerr << "Failed to read DLL file\n";
return 1;
}
HMEMORYMODULE mod = MemoryLoadLibrary(buffer.data(), buffer.size());
if (!mod)
{
std::cerr << "MemoryLoadLibrary failed\n";
return 1;
}
HelloFunc hello = (HelloFunc)MemoryGetProcAddress(mod, "HelloFromDll");
if (!hello)
{
std::cerr << "Could not find HelloFromDll\n";
MemoryFreeLibrary(mod);
return 1;
}
if (!hello())
{
std::cerr << "HelloFromDll returned failure\n";
MemoryFreeLibrary(mod);
return 1;
}
MemoryFreeLibrary(mod);
}
{
// Read the EXE from disk into a buffer (just for demo)
std::ifstream file("TestExe.exe", std::ios::binary);
std::vector<char> buffer((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
if (buffer.empty())
{
std::cerr << "Failed to read EXE file\n";
return 1;
}
HMEMORYMODULE mod = MemoryLoadLibrary(buffer.data(), buffer.size());
if (!mod)
{
std::cerr << "MemoryLoadLibrary failed\n";
return 1;
}
SetEnvironmentVariableA("MEMORYMODULE_TEST_EXE_RAN", NULL);
if (MemoryCallEntryPoint(mod) != 0)
{
std::cerr << "MemoryCallEntryPoint failed\n";
MemoryFreeLibrary(mod);
return 1;
}
char envValue[8] = {};
DWORD envLen = GetEnvironmentVariableA("MEMORYMODULE_TEST_EXE_RAN", envValue, sizeof(envValue));
if (envLen == 0 || strcmp(envValue, "true") != 0)
{
std::cerr << "TestExe did not mark execution success\n";
MemoryFreeLibrary(mod);
return 1;
}
MemoryFreeLibrary(mod);
}
std::cout << "Finished OK" << std::endl;
return 0;
}