-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallDLLInterfaceFunction.cpp
More file actions
304 lines (263 loc) · 9.75 KB
/
CallDLLInterfaceFunction.cpp
File metadata and controls
304 lines (263 loc) · 9.75 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//---------------------------------------------------------------------------
#pragma hdrstop
#include "CallDLLInterfaceFunction.h"
CallDLLInterface::CallDLL GlobalAppCallDLL;
#include <imagehlp.hpp>
bool CallDLLInterface::ListDLLFunctions(AnsiString sADllName, TStringList *slListOfDllFunctions)
{
bool Return = true;
DWORD *dNameRVAs = (DWORD *)calloc(1048575, sizeof(DWORD));
_IMAGE_EXPORT_DIRECTORY *ImageExportDirectory;
_IMAGE_SECTION_HEADER *ImageSectionHeader;
unsigned long cDirSize;
_LOADED_IMAGE LoadedImage;
AnsiString sName;
slListOfDllFunctions->Clear();
if (MapAndLoad(sADllName.c_str(), NULL, &LoadedImage, True, True))
{
try{
ImageExportDirectory = (_IMAGE_EXPORT_DIRECTORY *)ImageDirectoryEntryToData(LoadedImage.MappedAddress,
false, IMAGE_DIRECTORY_ENTRY_EXPORT, &cDirSize);
if (ImageExportDirectory != NULL)
{
dNameRVAs = (DWORD *)ImageRvaToVa(LoadedImage.FileHeader, LoadedImage.MappedAddress,
DWORD(ImageExportDirectory->AddressOfNames), &ImageSectionHeader);
for (int i = 0; i < ImageExportDirectory->NumberOfNames; i++)
{
sName = (char *)ImageRvaToVa(LoadedImage.FileHeader, LoadedImage.MappedAddress,
dNameRVAs[i], &ImageSectionHeader);
slListOfDllFunctions->Add(sName);
}
}
}
__finally {
UnMapAndLoad(&LoadedImage);
}
}
//did any functions get added?
Return = (slListOfDllFunctions->Count > 0);
return Return;
}
AnsiString __fastcall CallDLLInterface::CallDLL::Str_Function_Str(char* DLL, char* Function, char* arg1)
{
String Result;
HANDLE DLLHandle = HashedLibs.LoadHashedLibrary(DLL); // Define the path string
if (DLLHandle != NULL)
{
typedef char* (*aDLLInterfaceFunction)(char*); // This is a function pointer
aDLLInterfaceFunction DLLInterfaceFunction =
(aDLLInterfaceFunction)GetProcAddress(DLLHandle, Function);
// Leading underscore required by DLL and caller compiler settings
char* cs_result;
if (DLLInterfaceFunction != NULL)
{
try
{
cs_result = DLLInterfaceFunction(arg1);
Result = String(cs_result);
}
catch(...)
{
}
}
else //function does not exist in this plugin
{
throw "Function does not exist in plugin";
}
//FreeLibrary(DLLHandle);
};
return Result;
}
AnsiString __fastcall CallDLLInterface::CallDLL::Str_Function_Int(char* DLL, char* Function, int arg1)
{
String Result;
HANDLE DLLHandle = HashedLibs.LoadHashedLibrary(DLL); // Define the path string
if (DLLHandle != NULL)
{
typedef char* (*aDLLInterfaceFunction)(int); // This is a function pointer
aDLLInterfaceFunction DLLInterfaceFunction =
(aDLLInterfaceFunction)GetProcAddress(DLLHandle, Function);
// Leading underscore required by DLL and caller compiler settings
char* cs_result;
if (DLLInterfaceFunction != NULL)
{
try
{
cs_result = DLLInterfaceFunction(arg1);
Result = String(cs_result);
}
catch(...)
{
}
}
else //function does not exist in this plugin
{
throw "Function does not exist in plugin";
}
//FreeLibrary(DLLHandle);
};
return Result;
}
int __fastcall CallDLLInterface::CallDLL::Int_Function(char* DLL, char* Function)
{
HANDLE DLLHandle = HashedLibs.LoadHashedLibrary(DLL); // Define the path string
int result;
if (DLLHandle != NULL)
{
typedef int (*aDLLInterfaceFunction)(void); // This is a function pointer
aDLLInterfaceFunction DLLInterfaceFunction =
(aDLLInterfaceFunction)GetProcAddress(DLLHandle, Function);
// Leading underscore required by DLL and caller compiler settings
if (DLLInterfaceFunction != NULL)
{
try
{
result = DLLInterfaceFunction();
}
catch(...)
{
}
}
else //function does not exist in this plugin
{
throw "Function does not exist in plugin";
}
//FreeLibrary(DLLHandle);
};
return result;
}
bool CallDLLInterface::AddIfDLLFunctionExists(HANDLE DLLHandle, char* Function, TStringList* slListOfDllFunctions)
{
LowLevelDLLFunction lowDLLfunc(DLLHandle, Function);
if (lowDLLfunc.farproc != NULL)
{
slListOfDllFunctions->Add(Function);
return true;
}
else return false;
}
void CallDLLInterface::PopulateHashedProcs(AnsiString sADllName,
TStringList* slListOfDllFunctions, HashedProcAdddresses& HashedDLLFuncsToPopulate)
{
//define an alias
std::map< std::string ,std::map<std::string, LowLevelDLLFunction> > & loadedFuncs =
HashedDLLFuncsToPopulate.loaded_dll_to_funcs;
for (int i = 0; i < slListOfDllFunctions->Count; i++)
{
std::string functionName = slListOfDllFunctions->Strings[i].c_str();
loadedFuncs[sADllName.c_str()][functionName] = LowLevelDLLFunction(
GlobalAppCallDLL.HashedLibs.LoadHashedLibrary(sADllName.c_str()),
functionName.c_str());
//now go ahead add to the function -> dll list map
HashedDLLFuncsToPopulate.loaded_func_to_dlls[functionName].push_back(
loadedFuncs[sADllName.c_str()][functionName]);
}
}
//---------------------------------------------------------------------------
//get a DLL function pointer , try to load it if it doesnt exist in memory,
// return NULL if either the DLL fails to load, or the Function does not exist in the DLL
FARPROC CallDLLInterface::CallDLL::GetFunction(const char* DLL, const char* Function, LowLevelDLLFunction* lowDLLfunc)
{
//define an alias
std::map< std::string ,std::map<std::string, LowLevelDLLFunction> > & loadedFuncs =
HashedDLLFuncs.loaded_dll_to_funcs;
if (loadedFuncs.find(DLL) != loadedFuncs.end())
{
//the DLL exists, now is the function a part of the dll?
if (loadedFuncs[DLL].find(Function) != loadedFuncs[DLL].end()
/*&& loadedFuncs[DLL][Function].farproc != NULL*/ )
{
if (lowDLLfunc){ *lowDLLfunc = LowLevelDLLFunction(loadedFuncs[DLL][Function]); }
//the function is in this DLL (or so it says it is)
return loadedFuncs[DLL][Function].farproc;
}
//DLL is stored, but the function cannot be found!
//Warning* the programmer may not have populated the loaded functions!
else return NULL;
}
else //dll does not exist, try to load it
{
HANDLE DLLHandle = HashedLibs.LoadHashedLibrary(DLL);
if (DLLHandle)
{
loadedFuncs[DLL] = map< std::string , LowLevelDLLFunction>();
FARPROC func = GetProcAddress(DLLHandle, Function);
if (func != NULL)
{
//function actually exists in the DLL! yay!
loadedFuncs[DLL][Function].handle = DLLHandle;
loadedFuncs[DLL][Function].farproc = func;
if (lowDLLfunc){ *lowDLLfunc = LowLevelDLLFunction(loadedFuncs[DLL][Function]); }
//go ahead and populate the func_to_dlls list also with this info!
HashedDLLFuncs.loaded_func_to_dlls[Function].push_back( loadedFuncs[DLL][Function] );
}
return func;
}
else return NULL; //could not load DLL!!
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall CallDLLInterface::CallDLL::Str_Function(char* DLL, char* Function)
{
String Result;
HANDLE DLLHandle = HashedLibs.LoadHashedLibrary(DLL); // Define the path string
if (DLLHandle)
{
typedef char* (*aDLLInterfaceFunction)(void); // This is a function pointer
aDLLInterfaceFunction DLLInterfaceFunction =
(aDLLInterfaceFunction)GetProcAddress(DLLHandle, Function);
// Leading underscore required by DLL and caller compiler settings
char* cs_result;
if (DLLInterfaceFunction != NULL)
{
try
{
cs_result = DLLInterfaceFunction();
Result = String(cs_result);
}
catch(...)
{
}
}
else //function does not exist in this plugin
{
throw "Function does not exist in plugin";
}
//FreeLibrary(DLLHandle);
}
return Result;
}
//--------------------------------------------
HANDLE CallDLLInterface::HashedLibraries::LoadHashedLibrary(std::string DLL)
{
if (loaded_libs.find(DLL) == loaded_libs.end() || loaded_libs[DLL] == NULL)
{
loaded_libs[DLL] = LoadLibrary(DLL.c_str());
}
return loaded_libs[DLL];
}
bool CallDLLInterface::HashedLibraries::FreeHashedLibrary(std::string DLL)
{
if (loaded_libs.find(DLL) != loaded_libs.end() && loaded_libs[DLL] != NULL)
{
FreeLibrary(loaded_libs[DLL]);
loaded_libs.erase(loaded_libs.find(DLL));
return true;
}
return false; //does not exist in map!
}
//do this before program exits, or if we want to refresh the loaded libraries
void CallDLLInterface::HashedLibraries::FreeHashedLibraries()
{
map<string, HANDLE>::iterator i;
map<string, HANDLE>::iterator end;
for (i = loaded_libs.begin(), end = loaded_libs.end(); i != end; ++i)
{
if (i->second != NULL)
{
FreeLibrary(i->second);
i->second = NULL;
}
}
loaded_libs.erase(loaded_libs.begin(), loaded_libs.end());
}