GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
chaiscript_windows.hpp
Go to the documentation of this file.
1// This file is distributed under the BSD License.
2// See "license.txt" for details.
3// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5// http://www.chaiscript.com
6
7#ifndef CHAISCRIPT_WINDOWS_HPP_
8#define CHAISCRIPT_WINDOWS_HPP_
9
10#include <string>
11
12#ifdef CHAISCRIPT_WINDOWS
13#define VC_EXTRA_LEAN
14#if !defined(WIN32_LEAN_AND_MEAN)
15#define WIN32_LEAN_AND_MEAN
16#endif
17#include <windows.h>
18#endif
19
20
21namespace chaiscript
22{
23 namespace detail
24 {
25 struct Loadable_Module
26 {
27 template<typename T>
28 static std::wstring to_wstring(const T &t_str)
29 {
30 return std::wstring(t_str.begin(), t_str.end());
31 }
32
33 template<typename T>
34 static std::string to_string(const T &t_str)
35 {
36 return std::string(t_str.begin(), t_str.end());
37 }
38
39#if defined(_UNICODE) || defined(UNICODE)
40 template<typename T>
41 static std::wstring to_proper_string(const T &t_str)
42 {
43 return to_wstring(t_str);
44 }
45#else
46 template<typename T>
47 static std::string to_proper_string(const T &t_str)
48 {
49 return to_string(t_str);
50 }
51#endif
52
53 static std::string get_error_message(DWORD t_err)
54 {
55 typedef LPTSTR StringType;
56
57#if defined(_UNICODE) || defined(UNICODE)
58 std::wstring retval = L"Unknown Error";
59#else
60 std::string retval = "Unknown Error";
61#endif
62 StringType lpMsgBuf = nullptr;
63
64 if (FormatMessage(
65 FORMAT_MESSAGE_ALLOCATE_BUFFER |
66 FORMAT_MESSAGE_FROM_SYSTEM |
67 FORMAT_MESSAGE_IGNORE_INSERTS,
68 nullptr,
69 t_err,
70 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
71 reinterpret_cast<StringType>(&lpMsgBuf),
72 0, nullptr ) != 0 && lpMsgBuf)
73 {
74 retval = lpMsgBuf;
75 LocalFree(lpMsgBuf);
76 }
77
78 return to_string(retval);
79 }
80
81 struct DLModule
82 {
83 explicit DLModule(const std::string &t_filename)
84 : m_data(LoadLibrary(to_proper_string(t_filename).c_str()))
85 {
86 if (!m_data)
87 {
89 }
90 }
91
92 DLModule(DLModule &&) = default;
93 DLModule &operator=(DLModule &&) = default;
94 DLModule(const DLModule &) = delete;
95 DLModule &operator=(const DLModule &) = delete;
96
98 {
99 FreeLibrary(m_data);
100 }
101
102 HMODULE m_data;
103 };
104
105 template<typename T>
106 struct DLSym
107 {
108 DLSym(DLModule &t_mod, const std::string &t_symbol)
109 : m_symbol(reinterpret_cast<T>(GetProcAddress(t_mod.m_data, t_symbol.c_str())))
110 {
111 if (!m_symbol)
112 {
114 }
115 }
116
117 T m_symbol;
118 };
119
120 Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
121 : m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
122 m_moduleptr(m_func.m_symbol())
123 {
124 }
125
126 DLModule m_dlmodule;
127 DLSym<Create_Module_Func> m_func;
129 };
130 }
131}
132#endif
133
Classes and functions reserved for internal use. Items in this namespace are not supported.
Namespace chaiscript contains every API call that the average user will be concerned with.
std::shared_ptr< Module > ModulePtr
Convenience typedef for Module objects to be added to the ChaiScript runtime.
Definition dispatchkit.hpp:263
Definition GameApi.h:44
Definition GameApi.h:42
Definition chaiscript_posix.hpp:17
~DLModule()
Definition chaiscript_windows.hpp:97
void * m_data
Definition chaiscript_posix.hpp:37
DLModule & operator=(const DLModule &)=delete
DLModule & operator=(DLModule &&)=default
DLModule(const std::string &t_filename)
Definition chaiscript_windows.hpp:83
Definition chaiscript_posix.hpp:42
DLSym(DLModule &t_mod, const std::string &t_symbol)
Definition chaiscript_windows.hpp:108
T m_symbol
Definition chaiscript_posix.hpp:52
Definition chaiscript_posix.hpp:15
DLModule m_dlmodule
Definition chaiscript_posix.hpp:61
ModulePtr m_moduleptr
Definition chaiscript_posix.hpp:63
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
Definition chaiscript_windows.hpp:120
static std::wstring to_wstring(const T &t_str)
Definition chaiscript_windows.hpp:28
static std::string to_string(const T &t_str)
Definition chaiscript_windows.hpp:34
DLSym< Create_Module_Func > m_func
Definition chaiscript_posix.hpp:62
static std::string get_error_message(DWORD t_err)
Definition chaiscript_windows.hpp:53
static std::string to_proper_string(const T &t_str)
Definition chaiscript_windows.hpp:47
Thrown if an error occurs while attempting to load a binary module.
Definition chaiscript_common.hpp:138