BlackBone
Windows memory hacking library
 All Classes Functions
DynImport.h
1 #pragma once
2 
3 #include "../Include/Types.h"
4 #include "../Include/Winheaders.h"
5 #include "Utils.h"
6 
7 #include <unordered_map>
8 
9 namespace blackbone
10 {
11 
15 class DynImport
16 {
17 public:
23  template<typename T>
24  inline static T get( const std::string& name )
25  {
26  CSLock lck( _mapGuard );
27 
28  auto iter = _funcs.find( name );
29  if (iter != _funcs.end())
30  return reinterpret_cast<T>(iter->second);
31 
32  return nullptr;
33  }
34 
42  template<typename T, typename... Args>
43  inline static NTSTATUS safeNativeCall( const std::string& name, Args... args )
44  {
45  auto pfn = DynImport::get<T>( name );
46  return pfn ? pfn( std::forward<Args>( args )... ) : STATUS_ORDINAL_NOT_FOUND;
47  }
48 
56  template<typename T, typename... Args>
57  inline static auto safeCall( const std::string& name, Args... args ) -> typename std::result_of<T(Args...)>::type
58  {
59  auto pfn = DynImport::get<T>( name );
60  return pfn ? pfn( std::forward<Args>( args )... ) : (std::result_of<T( Args... )>::type )(0);
61  }
62 
69  BLACKBONE_API static FARPROC load( const std::string& name, const std::wstring& module );
70 
77  BLACKBONE_API static FARPROC load( const std::string& name, HMODULE hMod );
78 
79 private:
80  BLACKBONE_API static std::unordered_map<std::string, FARPROC> _funcs; // function database
81  BLACKBONE_API static CriticalSection _mapGuard; // function database guard
82 };
83 
84 // Syntax sugar
85 #define GET_IMPORT(name) (DynImport::get<fn ## name>( #name ))
86 #define SAFE_NATIVE_CALL(name, ...) (DynImport::safeNativeCall<fn ## name>( #name, __VA_ARGS__ ))
87 #define SAFE_CALL(name, ...) (DynImport::safeCall<fn ## name>( #name, __VA_ARGS__ ))
88 
89 }
std::lock_guard alternative
Definition: Utils.h:134
Dynamic import
Definition: DynImport.h:15
static BLACKBONE_API FARPROC load(const std::string &name, const std::wstring &module)
Load function into database
Definition: DynImport.cpp:15
static NTSTATUS safeNativeCall(const std::string &name, Args...args)
Safely call import If import not found - return STATUS_ORDINAL_NOT_FOUND
Definition: DynImport.h:43
std::mutex alternative
Definition: Utils.h:103
static auto safeCall(const std::string &name, Args...args) -> typename std::result_of< T(Args...)>::type
Safely call import If import not found - return 0
Definition: DynImport.h:57
Definition: AsmHelper32.cpp:6