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  {
27  h_ = LoadLibraryExW(name, nullptr, flags);
28  }
29 
31  ~dll() {
32  if (h_) {
33  FreeLibrary(h_);
34  }
35  }
36 
37  template<typename T>
38  static dll from_address(T const& t) {
39  dll d;
40  if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char const*>(t), &d.h_)) {
41  d.h_ = nullptr;
42  }
43  return d;
44  }
45 
46  dll(dll const&) = delete;
47  dll& operator=(dll const&) = delete;
48 
49  dll(dll && d);
50  dll& operator=(dll && d);
51 
52  explicit operator bool() const {
53  return h_ != nullptr;
54  }
55 
61  void *operator[](char const *name) {
62  return h_ ? reinterpret_cast<void*>(::GetProcAddress(h_, name)) : nullptr;
63  }
64 
65 private:
66  HMODULE h_{};
67 };
68 
73 class FZ_PUBLIC_SYMBOL shdlls final
74 {
75 protected:
76  shdlls();
77  ~shdlls();
78 
79  shdlls(shdlls const&) = delete;
80  shdlls* operator=(shdlls const&) = delete;
81 
82 public:
83  static shdlls& get();
84 
87 };
88 
89 }
90 
91 #else
92 #error This file is for Windows only
93 #endif
94 
95 #endif
96 
dll(wchar_t const *name, DWORD flags)
Open the specified library with the passed in flags.
Definition: dll.hpp:25
void * operator[](char const *name)
Retrieves the address of an exported symbol in the library.
Definition: dll.hpp:61
dll ole32_
The Ole32 DLL.
Definition: dll.hpp:86
~dll()
Closes the library and frees related resources.
Definition: dll.hpp:31
A collection of commonly used dlls.
Definition: dll.hpp:73
dll shell32_
The Shell32 DLL.
Definition: dll.hpp:85
The namespace used by libfilezilla.
Definition: apply.hpp:17
Encapsulates a DLL.
Definition: dll.hpp:17