@@ -11,17 +11,21 @@ namespace lime
1111{
1212 struct module ::impl
1313 {
14+ using callback_t = std::function<bool (const std::string &)>;
15+
16+ public:
1417 HMODULE handle;
1518
1619 public:
1720 MODULEINFO info;
1821 std::string name;
1922
2023 public:
21- static std::string lower (const std::string &string) ;
24+ void iterate_symbols (const callback_t &) const ;
2225
2326 public:
24- void iterate_symbols (const std::function<bool (const std::string &)> &) const ;
27+ static std::optional<module > get (HMODULE module );
28+ static std::string lower (const std::string &string);
2529 };
2630
2731 module::module () :m_impl(std::make_unique<impl>()) {}
@@ -116,45 +120,41 @@ namespace lime
116120
117121 for (auto i = 0u ; (modules_size / sizeof (HMODULE)) > i; i++)
118122 {
119- CHAR name[MAX_PATH] ;
123+ auto module = impl::get (modules[i]) ;
120124
121- if (!GetModuleBaseNameA ( GetCurrentProcess (), modules[i], name, MAX_PATH) )
125+ if (!module )
122126 {
123127 continue ;
124128 }
125129
126- MODULEINFO info;
127-
128- if (!GetModuleInformation (GetCurrentProcess (), modules[i], &info, sizeof (info)))
129- {
130- continue ;
131- }
132-
133- lime::module item;
134-
135- item.m_impl ->info = info;
136- item.m_impl ->handle = modules[i];
137- item.m_impl ->name = impl::lower (name);
138-
139- rtn.emplace_back (std::move (item));
130+ rtn.emplace_back (std::move (module .value ()));
140131 }
141132
142133 return rtn;
143134 }
144135
145136 std::optional<module > module::get (const std::string &name)
146137 {
147- auto all = modules ();
148- const auto lower = impl::lower (name);
138+ auto *module = GetModuleHandleA (name.c_str ());
139+
140+ if (!module )
141+ {
142+ return std::nullopt ;
143+ }
149144
150- auto it = std::find_if (all.begin (), all.end (), [&](auto &item) { return item.name () == lower; });
145+ return impl::get (module );
146+ }
151147
152- if (it == all.end ())
148+ std::optional<module > module::load (const std::string &name)
149+ {
150+ auto *module = LoadLibraryA (name.c_str ());
151+
152+ if (!module )
153153 {
154154 return std::nullopt ;
155155 }
156156
157- return std::move (*it );
157+ return impl::get ( module );
158158 }
159159
160160 std::optional<module > module::find (const std::string &name)
@@ -173,14 +173,6 @@ namespace lime
173173 return std::move (*it);
174174 }
175175
176- std::string module::impl::lower (const std::string &string)
177- {
178- auto rtn{string};
179- std::transform (rtn.begin (), rtn.end (), rtn.begin (), [](unsigned char c) { return std::tolower (c); });
180-
181- return rtn;
182- };
183-
184176 void module::impl::iterate_symbols (const std::function<bool (const std::string &)> &callback) const
185177 {
186178 CHAR path[MAX_PATH];
@@ -219,4 +211,37 @@ namespace lime
219211
220212 UnMapAndLoad (&image);
221213 }
214+
215+ std::optional<module > module::impl::get (HMODULE module )
216+ {
217+ CHAR name[MAX_PATH];
218+
219+ if (!GetModuleBaseNameA (GetCurrentProcess (), module , name, MAX_PATH))
220+ {
221+ return std::nullopt ;
222+ }
223+
224+ MODULEINFO info;
225+
226+ if (!GetModuleInformation (GetCurrentProcess (), module , &info, sizeof (info)))
227+ {
228+ return std::nullopt ;
229+ }
230+
231+ lime::module item;
232+
233+ item.m_impl ->info = info;
234+ item.m_impl ->handle = module ;
235+ item.m_impl ->name = lower (name);
236+
237+ return item;
238+ }
239+
240+ std::string module::impl::lower (const std::string &string)
241+ {
242+ auto rtn{string};
243+ std::transform (rtn.begin (), rtn.end (), rtn.begin (), [](unsigned char c) { return std::tolower (c); });
244+
245+ return rtn;
246+ };
222247} // namespace lime
0 commit comments