forked from mayossi/Dynamic-Windows-API-Resolver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dllutils.cpp
40 lines (34 loc) · 1.47 KB
/
dllutils.cpp
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
#include "dllutils.h"
#include <iostream>
std::unordered_map<std::string, HMODULE>& DllUtils::getModuleCache() {
static std::unordered_map<std::string, HMODULE> moduleCache;
return moduleCache;
}
std::unordered_map<std::string, std::unordered_map<std::string, FARPROC>>& DllUtils::getFunctionCache() {
static std::unordered_map<std::string, std::unordered_map<std::string, FARPROC>> functionCache;
return functionCache;
}
std::function<void()> DllUtils::loadFunction(const std::string& moduleName, const std::string& procName) {
auto& moduleCache = getModuleCache();
auto& functionCache = getFunctionCache();
auto moduleIt = moduleCache.find(moduleName);
if (moduleIt == moduleCache.end()) {
HMODULE hModule = LoadLibraryA(moduleName.c_str());
if (!hModule) {
std::cout << "Failed to load module: " << moduleName << std::endl;
return nullptr;
}
moduleIt = moduleCache.emplace(moduleName, hModule).first;
}
auto& funcCache = functionCache[moduleName];
auto funcIt = funcCache.find(procName);
if (funcIt == funcCache.end()) {
FARPROC procAddr = GetProcAddress(moduleIt->second, procName.c_str());
if (!procAddr) {
std::cout << "Failed to get address of function: " << procName << std::endl;
return nullptr;
}
funcIt = funcCache.emplace(procName, procAddr).first;
}
return reinterpret_cast<std::function<void()>>(funcIt->second);
}