libfilezilla
dll.hpp
1 #ifndef LIBFILEZILLA_GLUE_DLL_HEADER
2 #define LIBFILEZILLA_GLUE_DLL_HEADER
3 
4 #include "../libfilezilla.hpp"
5 
6 #ifdef FZ_WINDOWS
7 
8 #include "./windows.hpp"
9 
10 namespace fz {
11 
17 class FZ_PUBLIC_SYMBOL dll final
18 {
19 public:
20  explicit dll()
21  : h_{}
22  {}
23 
25  explicit dll(wchar_t const* name, DWORD flags)
26  : h_{LoadLibraryExW(name, nullptr, flags)}
27  {}
28 
30  ~dll() {
31  if (h_) {
32  FreeLibrary(h_);
33  }
34  }
35 
36  template<typename T>
37  static dll from_address(T const& t) {
38  dll d;
39  if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char const*>(t), &d.h_)) {
40  d.h_ = nullptr;
41  }
42  return d;
43  }
44 
45  dll(dll const&) = delete;
46  dll& operator=(dll const&) = delete;
47 
48  dll(dll && d);
49  dll& operator=(dll && d);
50 
51  explicit operator bool() const {
52  return h_ != nullptr;
53  }
54 
60  void *operator[](char const *name) const {
61  return h_ ? reinterpret_cast<void*>(::GetProcAddress(h_, name)) : nullptr;
62  }
63 
64 private:
65  mutable HMODULE h_{};
66 };
67 
72 class FZ_PUBLIC_SYMBOL shdlls final
73 {
74 protected:
75  shdlls();
76  ~shdlls();
77 
78  shdlls(shdlls const&) = delete;
79  shdlls* operator=(shdlls const&) = delete;
80 
81 public:
82  static shdlls& get();
83 
86 };
87 
88 }
89 
115 #define FZ_DLL_IMPORT(dll, symbol) \
116  static const auto symbol = reinterpret_cast<decltype(&::symbol)>((dll)[#symbol]) \
117 /***/
118 
119 #else
120 #error This file is for Windows only
121 #endif
122 
123 #endif
124 
dll(wchar_t const *name, DWORD flags)
Open the specified library with the passed in flags.
Definition: dll.hpp:25
dll ole32_
The Ole32 DLL.
Definition: dll.hpp:85
~dll()
Closes the library and frees related resources.
Definition: dll.hpp:30
void * operator[](char const *name) const
Retrieves the address of an exported symbol in the library.
Definition: dll.hpp:60
A collection of commonly used dlls.
Definition: dll.hpp:72
dll shell32_
The Shell32 DLL.
Definition: dll.hpp:84
The namespace used by libfilezilla.
Definition: apply.hpp:17
Encapsulates a DLL.
Definition: dll.hpp:17