diff --git a/example/getting_started.cpp b/example/getting_started.cpp old mode 100644 new mode 100755 index b9bd973..dae1517 --- a/example/getting_started.cpp +++ b/example/getting_started.cpp @@ -9,13 +9,13 @@ #include #include #include +#include "b2_workarounds.hpp" // Unit Tests int main(int argc, char* argv[]) { using namespace boost; - BOOST_TEST(argc >= 2); - filesystem::path path_to_shared_library = argv[1]; + filesystem::path path_to_shared_library = b2_workarounds::first_lib_from_argv(argc, argv); BOOST_TEST(path_to_shared_library.string().find("getting_started_library") != std::string::npos); //[getting_started_imports_c_function diff --git a/example/tutorial6/tutorial6.cpp b/example/tutorial6/tutorial6.cpp old mode 100644 new mode 100755 index 3ad2282..68f4420 --- a/example/tutorial6/tutorial6.cpp +++ b/example/tutorial6/tutorial6.cpp @@ -5,6 +5,8 @@ // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) +#include "../b2_workarounds.hpp" + //[callplugcpp_tutorial6 #include #include @@ -17,9 +19,8 @@ void print_unloaded() { } int main(int argc, char* argv[]) { - /*<-*/ BOOST_ASSERT(argc >= 2); (void)argc; /*->*/ // argv[1] contains full path to our plugin library - boost::filesystem::path shared_library_path = argv[1]; + boost::filesystem::path shared_library_path = /*<-*/ b2_workarounds::first_lib_from_argv(argc, argv); /*->*/ //=argv[1]; // loading library and getting a function from it boost::function on_unload diff --git a/example/tutorial9/tutorial9.cpp b/example/tutorial9/tutorial9.cpp old mode 100644 new mode 100755 index 2f07056..7ff2978 --- a/example/tutorial9/tutorial9.cpp +++ b/example/tutorial9/tutorial9.cpp @@ -8,7 +8,8 @@ #if BOOST_OS_WINDOWS //[callplugcpp_tutorial9 -#include // for import +#include // for dll::import +#include // for dll::shared_library #include #include #include @@ -20,7 +21,7 @@ int main() { // OPTION #0, requires C++11 compatible compiler that understands GetStdHandle_t signature. /*<-*/ -#if !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) /*->*/ +#if defined(_MSC_VER) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) /*->*/ auto get_std_handle = dll::import( "Kernel32.dll", "GetStdHandle" diff --git a/include/boost/dll/detail/windows/path_from_handle.hpp b/include/boost/dll/detail/windows/path_from_handle.hpp old mode 100644 new mode 100755 index 0372b75..b97c5e6 --- a/include/boost/dll/detail/windows/path_from_handle.hpp +++ b/include/boost/dll/detail/windows/path_from_handle.hpp @@ -1,4 +1,5 @@ // Copyright 2014 Renato Tegon Forti, Antony Polukhin. +// Copyright 2015 Antony Polukhin. // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt @@ -19,7 +20,6 @@ namespace boost { namespace dll { namespace detail { - static inline boost::system::error_code last_error_code() BOOST_NOEXCEPT { return boost::system::error_code( boost::detail::winapi::GetLastError(), @@ -28,42 +28,31 @@ namespace boost { namespace dll { namespace detail { } inline boost::filesystem::path path_from_handle(boost::detail::winapi::HMODULE_ handle, boost::system::error_code &ec) { - boost::filesystem::path ret; - BOOST_STATIC_CONSTANT(boost::detail::winapi::DWORD_, ERROR_INSUFFICIENT_BUFFER_ = 0x7A); BOOST_STATIC_CONSTANT(boost::detail::winapi::DWORD_, DEFAULT_PATH_SIZE_ = 260); - // A handle to the loaded module whose path is being requested. - // If this parameter is NULL, GetModuleFileName retrieves the path of the + // If `handle` parameter is NULL, GetModuleFileName retrieves the path of the // executable file of the current process. boost::detail::winapi::WCHAR_ path_hldr[DEFAULT_PATH_SIZE_]; - boost::detail::winapi::LPWSTR_ path = path_hldr; - boost::detail::winapi::GetModuleFileNameW(handle, path, DEFAULT_PATH_SIZE_); + boost::detail::winapi::GetModuleFileNameW(handle, path_hldr, DEFAULT_PATH_SIZE_); ec = last_error_code(); + if (!ec) { + return boost::filesystem::path(path_hldr); + } - // In case of ERROR_INSUFFICIENT_BUFFER_ trying to get buffer big enough to store the whole path - for (unsigned i = 2; i < 1025 && ec.value() == ERROR_INSUFFICIENT_BUFFER_; i *= 2) { - path = new boost::detail::winapi::WCHAR_[DEFAULT_PATH_SIZE_ * i]; - - boost::detail::winapi::GetModuleFileNameW(handle, path, DEFAULT_PATH_SIZE_ * i); + for (unsigned i = 2; i < 1025 && static_cast(ec.value()) == ERROR_INSUFFICIENT_BUFFER_; i *= 2) { + std::wstring p(DEFAULT_PATH_SIZE_ * i, L'\0'); + const std::size_t size = boost::detail::winapi::GetModuleFileNameW(handle, &p[0], DEFAULT_PATH_SIZE_ * i); ec = last_error_code(); - if (ec) { - delete[] path; + if (!ec) { + p.resize(size); + return boost::filesystem::path(p); } } - - if (ec) { - // Error other than ERROR_INSUFFICIENT_BUFFER_ occurred or failed to allocate buffer big enough - return boost::filesystem::path(); - } - - ret = path; - if (path != path_hldr) { - delete[] path; - } - - return ret; + + // Error other than ERROR_INSUFFICIENT_BUFFER_ occurred or failed to allocate buffer big enough + return boost::filesystem::path(); } }}} // namespace boost::dll::detail