forked from FusixGit/MemoryModule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDllLoader.cpp
More file actions
115 lines (90 loc) · 2.9 KB
/
DllLoader.cpp
File metadata and controls
115 lines (90 loc) · 2.9 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#define WIN32_LEAN_AND_MEAN
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <malloc.h>
#include "../../MemoryModule.h"
typedef int (*addNumberProc)(int, int);
#define DLL_FILE TEXT("..\\SampleDLL\\SampleDLL.dll")
void LoadFromFile(void)
{
addNumberProc addNumber;
HRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
TCHAR buffer[100];
HINSTANCE handle = LoadLibrary(DLL_FILE);
if (handle == NULL)
return;
addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
_tprintf(_T("From file: %d\n"), addNumber(1, 2));
resourceInfo = FindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
_tprintf(_T("FindResource returned 0x%p\n"), resourceInfo);
resourceSize = SizeofResource(handle, resourceInfo);
resourceData = LoadResource(handle, resourceInfo);
_tprintf(_T("Resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
LoadString(handle, 1, buffer, sizeof(buffer));
_tprintf(_T("String1: %s\n"), buffer);
LoadString(handle, 20, buffer, sizeof(buffer));
_tprintf(_T("String2: %s\n"), buffer);
FreeLibrary(handle);
}
void LoadFromMemory(void)
{
FILE *fp;
unsigned char *data=NULL;
long size;
size_t read;
HMEMORYMODULE handle;
addNumberProc addNumber;
HMEMORYRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
TCHAR buffer[100];
fp = _tfopen(DLL_FILE, _T("rb"));
if (fp == NULL)
{
_tprintf(_T("Can't open DLL file \"%s\"."), DLL_FILE);
goto exit;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
assert(size >= 0);
data = (unsigned char *)malloc(size);
assert(data != NULL);
fseek(fp, 0, SEEK_SET);
read = fread(data, 1, size, fp);
assert(read == static_cast<size_t>(size));
fclose(fp);
handle = MemoryLoadLibrary(data, size);
if (handle == NULL)
{
_tprintf(_T("Can't load library from memory.\n"));
goto exit;
}
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
_tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo);
resourceSize = MemorySizeofResource(handle, resourceInfo);
resourceData = MemoryLoadResource(handle, resourceInfo);
_tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
MemoryLoadString(handle, 1, buffer, sizeof(buffer));
_tprintf(_T("String1: %s\n"), buffer);
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
_tprintf(_T("String2: %s\n"), buffer);
MemoryFreeLibrary(handle);
exit:
free(data);
}
int main()
{
LoadFromFile();
printf("\n\n");
LoadFromMemory();
return 0;
}