source: nscp/include/dll/impl_w32.hpp @ d05c3f0

0.4.00.4.10.4.2
Last change on this file since d05c3f0 was d05c3f0, checked in by Michael Medin <michael@…>, 4 years ago

Next major "milestone" we now have one working plugin (CheckHelpers) which loads and works both on Linux and Windows.
This is (as always) a broken build which many many features disabled and non functional...

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#pragma once
2#include <unicode_char.hpp>
3#include <boost/noncopyable.hpp>
4#include <error.hpp>
5
6namespace dll {
7        namespace win32 {
8                class impl : public boost::noncopyable {
9                private:
10                        HMODULE handle_;
11                        boost::filesystem::wpath module_;
12                public:
13                        impl(boost::filesystem::wpath module) : module_(module), handle_(NULL) {
14                                if (!boost::filesystem::is_regular(module_)) {
15                                        module_ = fix_module_name(module_);
16                                }
17                        }
18                        boost::filesystem::wpath fix_module_name( boost::filesystem::wpath module_ ) {
19                                boost::filesystem::wpath mod = module_ / std::wstring(_T(".dll"));
20                                if (boost::filesystem::is_regular(mod))
21                                        return mod;
22                                std::wstring tmp = module_.file_string() + _T(".dll");
23                                mod = tmp;
24                                if (boost::filesystem::is_regular(mod))
25                                        return mod;
26                                return module_;
27                        }
28
29                        static bool is_module(std::wstring file) {
30                                return boost::ends_with(file, _T(".dll"));
31                        }
32
33                        void load_library() {
34                                if (handle_ != NULL)
35                                        unload_library();
36                                handle_ = LoadLibrary(module_.file_string().c_str());
37                                if (handle_ == NULL)
38                                        throw dll_exception(_T("Could not load library: ") + error::lookup::last_error() + _T(": ") + module_.file_string());
39                        }
40                        LPVOID load_proc(std::string name) {
41                                if (handle_ == NULL)
42                                        throw dll_exception(_T("Failed to load process since module is not loaded: ") + module_.file_string());
43                                LPVOID ep = GetProcAddress(handle_, name.c_str());
44                                return ep;
45                        }
46
47                        void unload_library() {
48                                if (handle_ == NULL)
49                                        return;
50                                FreeLibrary(handle_);
51                                handle_ = NULL;
52                        }
53                        bool is_loaded() const { return handle_ != NULL; }
54                        std::wstring get_file() const { return module_.file_string(); }
55                };
56        }
57}
58
59
Note: See TracBrowser for help on using the repository browser.