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

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

Builds again!!!
Streamlined settings and mde the client side API slightly nicer to work with.
Also migrated scheduler to new settings subsystem

  • Property mode set to 100644
File size: 2.0 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                        static boost::filesystem::wpath fix_module_name( boost::filesystem::wpath module ) {
19                                if (boost::filesystem::is_regular(module))
20                                        return module;
21                                boost::filesystem::wpath mod = module / std::wstring(_T(".dll"));
22                                if (boost::filesystem::is_regular(mod))
23                                        return mod;
24                                return module;
25                        }
26
27                        static bool is_module(std::wstring file) {
28                                return boost::ends_with(file, _T(".dll"));
29                        }
30
31                        void load_library() {
32                                if (handle_ != NULL)
33                                        unload_library();
34                                handle_ = LoadLibrary(module_.file_string().c_str());
35                                if (handle_ == NULL)
36                                        throw dll_exception(_T("Could not load library: ") + error::lookup::last_error() + _T(": ") + module_.file_string());
37                        }
38                        LPVOID load_proc(std::string name) {
39                                if (handle_ == NULL)
40                                        throw dll_exception(_T("Failed to load process since module is not loaded: ") + module_.file_string());
41                                LPVOID ep = GetProcAddress(handle_, name.c_str());
42                                return ep;
43                        }
44
45                        void unload_library() {
46                                if (handle_ == NULL)
47                                        return;
48                                FreeLibrary(handle_);
49                                handle_ = NULL;
50                        }
51                        bool is_loaded() const { return handle_ != NULL; }
52                        boost::filesystem::wpath get_file() const { return module_; }
53                        std::wstring get_filename() const { return module_.leaf(); }
54                        std::wstring get_module_name() {
55                                std::wstring ext = _T(".dll");
56                                int l = ext.length();
57                                std::wstring fn = get_filename();
58                                if ((fn.length() > l) && (fn.substr(fn.size()-l) == ext))
59                                        return fn.substr(0, fn.size()-l);
60                                return fn;
61                        }
62                };
63        }
64}
65
66
Note: See TracBrowser for help on using the repository browser.