library_win.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #include "rttr/detail/base/core_prerequisites.h"
  28. #if RTTR_PLATFORM == RTTR_PLATFORM_WINDOWS
  29. #include "rttr/detail/library/library_p.h"
  30. #include "rttr/detail/misc/utility.h"
  31. #include <iostream>
  32. #include <string>
  33. #include <locale>
  34. namespace
  35. {
  36. std::wstring convert_utf8_to_utf16(const std::string& source)
  37. {
  38. if (source.empty())
  39. return std::wstring();
  40. const auto size_needed = MultiByteToWideChar(CP_UTF8, 0, &source[0], static_cast<int>(source.size()), NULL, 0);
  41. std::wstring result(size_needed, 0);
  42. MultiByteToWideChar(CP_UTF8, 0, &source[0], static_cast<int>(source.size()), &result[0], size_needed);
  43. return result;
  44. }
  45. /////////////////////////////////////////////////////////////////////////////////////////
  46. std::string convert_utf16_to_utf8(const std::wstring& source)
  47. {
  48. if (source.empty())
  49. return std::string();
  50. const auto size_needed = WideCharToMultiByte(CP_UTF8, 0, &source[0], static_cast<int>(source.size()), NULL, 0, NULL, NULL);
  51. std::string result( size_needed, 0 );
  52. WideCharToMultiByte(CP_UTF8, 0, &source[0], static_cast<int>(source.size()), &result[0], size_needed, NULL, NULL);
  53. return result;
  54. }
  55. /////////////////////////////////////////////////////////////////////////////////////////
  56. std::string error_string(int error_code = -1)
  57. {
  58. std::string result;
  59. rttr::string_view std_error_msg;
  60. if (error_code == -1)
  61. {
  62. #if RTTR_PLATFORM == RTTR_PLATFORM_WINDOWS
  63. error_code = GetLastError();
  64. #else
  65. error_code = errno;
  66. #endif
  67. }
  68. switch (error_code)
  69. {
  70. case 0:
  71. break;
  72. case EACCES:
  73. std_error_msg = "Permission denied";
  74. break;
  75. case EMFILE:
  76. std_error_msg = "Too many open files";
  77. break;
  78. case ENOENT:
  79. std_error_msg = "No such file or directory";
  80. break;
  81. case ENOSPC:
  82. std_error_msg = "No space left on device";
  83. break;
  84. default:
  85. {
  86. wchar_t error_str[1024];
  87. const auto buffer_size =
  88. FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
  89. NULL,
  90. error_code,
  91. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  92. (LPWSTR)&error_str,
  93. sizeof(error_str) / sizeof(wchar_t),
  94. NULL);
  95. result = convert_utf16_to_utf8(std::wstring(error_str, error_str + buffer_size));
  96. break;
  97. }
  98. }
  99. if (!std_error_msg.empty())
  100. result = std_error_msg.to_string();
  101. return result;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////////////////
  104. #define IS_SEP(x) ((x) == L'/' || (x) == L'\\')
  105. #define PREF L'\\'
  106. #define COLON L':'
  107. // returns end of prefix
  108. size_t get_prefix_end(const std::wstring& path)
  109. {
  110. if (path.size() > 2
  111. && IS_SEP(path[0])
  112. && IS_SEP(path[1])
  113. && !IS_SEP(path[2]))
  114. { // network name => \\name
  115. std::size_t idx = 3;
  116. for (; idx < path.size() && !IS_SEP(path[idx]); ++idx)
  117. ;
  118. return idx;
  119. }
  120. else
  121. { // no network name => drive:
  122. std::size_t idx = path.find(COLON, 0);
  123. if (idx == path.npos)
  124. idx = 0;
  125. else
  126. ++idx;
  127. return idx;
  128. }
  129. }
  130. /////////////////////////////////////////////////////////////////////////////////////////
  131. std::wstring get_root_name(const std::wstring& path)
  132. {
  133. return path.substr(0, get_prefix_end(path));
  134. }
  135. /////////////////////////////////////////////////////////////////////////////////////////
  136. std::wstring get_root_dir(const std::wstring& path)
  137. {
  138. const auto idx = get_prefix_end(path);
  139. if (idx < path.size() && IS_SEP(path[idx]))
  140. return std::wstring(1, PREF);
  141. else
  142. return std::wstring();
  143. }
  144. /////////////////////////////////////////////////////////////////////////////////////////
  145. bool has_root_name(const std::wstring& path)
  146. {
  147. return !get_root_name(path).empty();
  148. }
  149. /////////////////////////////////////////////////////////////////////////////////////////
  150. bool has_root_dir(const std::wstring& path)
  151. {
  152. return !get_root_dir(path).empty();
  153. }
  154. /////////////////////////////////////////////////////////////////////////////////////////
  155. bool is_absolute_path(const std::wstring& path)
  156. {
  157. return (has_root_name(path) && has_root_dir(path));
  158. }
  159. } // end namespace anonymous
  160. namespace rttr
  161. {
  162. namespace detail
  163. {
  164. /////////////////////////////////////////////////////////////////////////////////////////
  165. bool library_private::load_native()
  166. {
  167. std::wstring wfile_name = convert_utf8_to_utf16(m_file_name);
  168. std::vector<std::wstring> paths_to_try;
  169. std::vector<std::wstring> prefix_list = {L"lib"};
  170. std::vector<std::wstring> suffix_list = {L".dll"};
  171. if (is_absolute_path(wfile_name))
  172. {
  173. // that means, the suffix and prefix should be ignored at first try
  174. prefix_list.insert(prefix_list.begin(), std::wstring());
  175. suffix_list.insert(suffix_list.begin(), std::wstring());
  176. }
  177. else
  178. {
  179. prefix_list.insert(prefix_list.begin(), std::wstring());
  180. suffix_list.push_back(std::wstring());
  181. }
  182. std::wstring attempt;
  183. auto retry = true;
  184. for (auto prefix = 0u; retry && !m_handle && prefix < prefix_list.size(); ++prefix)
  185. {
  186. for (auto suffix = 0u; retry && !m_handle && suffix < suffix_list.size(); ++suffix)
  187. {
  188. // don't attach prefix or suffix when it is already there
  189. if (!prefix_list[prefix].empty() && starts_with(wfile_name, prefix_list[prefix]))
  190. continue;
  191. if (!suffix_list[suffix].empty() && ends_with(wfile_name, suffix_list[suffix]))
  192. continue;
  193. attempt = prefix_list[prefix] + wfile_name + suffix_list[suffix];
  194. m_handle = LoadLibraryW(attempt.c_str());
  195. if (::GetLastError() != ERROR_MOD_NOT_FOUND)
  196. {
  197. retry = false;
  198. }
  199. }
  200. }
  201. if (!m_handle)
  202. {
  203. m_error_string = "Cannot load library: '" + m_file_name + "' : " + error_string();
  204. }
  205. else
  206. {
  207. m_error_string.clear();
  208. m_qualifed_file_name = convert_utf16_to_utf8(attempt);
  209. }
  210. return (m_handle != nullptr);
  211. }
  212. /////////////////////////////////////////////////////////////////////////////////////////
  213. bool library_private::unload_native()
  214. {
  215. if (!FreeLibrary(m_handle))
  216. {
  217. m_error_string = "Cannot unload library: " + m_file_name + " : " + error_string();
  218. return false;
  219. }
  220. return true;
  221. }
  222. /////////////////////////////////////////////////////////////////////////////////////////
  223. } // end namespace detail
  224. } // end namespace rttr
  225. #endif // RTTR_PLATFORM_WINDOWS