libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
function_resolver.cpp
Go to the documentation of this file.
2
3#include <iostream>
4#include <cctype>
5
6namespace util {
7 std::string toLowercase(std::string str)
8 {
9 for (char& ch : str) {
10 ch = std::tolower(static_cast<unsigned char>(ch));
11 }
12 return str;
13 }
14}; //namespace util
15
17{
18 if (!lib_name) {
19 return nullptr;
20 }
21 const std::string modName = util::toLowercase(lib_name);
22 auto found = this->nameToModule.find(modName);
23 if (found != this->nameToModule.end()) {
24 return found->second;
25 }
26 const HMODULE mod = LoadLibraryA(lib_name);
27 if (mod) {
28#ifdef _DEBUG
29 std::cout << "Loaded the DLL: " << lib_name << " : " << std::hex << mod << std::endl;
30#endif //_DEBUG
31 this->nameToModule[modName] = mod;
32 }
33 return mod;
34}
35
36FARPROC peconv::default_func_resolver::resolve_func(LPCSTR lib_name, LPCSTR func_name)
37{
38 HMODULE libBasePtr = load_library(lib_name);
39 if (libBasePtr == NULL) {
40 std::cerr << "Could not load the library: " << lib_name << std::endl;
41 return NULL;
42 }
43 FARPROC hProc = GetProcAddress(libBasePtr, func_name);
44 if (hProc == NULL) {
45 ULONGLONG func_val = (ULONGLONG)func_name;
46 //is only the first WORD filled?
47 bool is_ord = (func_val & (0x0FFFF)) == func_val;
48 std::cerr << "Could not load the function: " << lib_name << ".";
49 if (is_ord) {
50 std::cerr << std::hex << "0x" << func_val;
51 }
52 else {
53 std::cerr << func_name;
54 }
55 std::cerr << std::endl;
56 return NULL;
57 }
58 return hProc;
59}
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
std::map< std::string, HMODULE > nameToModule
virtual HMODULE load_library(LPCSTR lib_name)
Definitions of basic Imports Resolver classes. They can be used for filling imports when the PE is lo...
std::string toLowercase(std::string str)