// // process/this_process/detail/environment_win.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2021 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP #define BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP #include #include #include #include #include #include #include #include #include #include BOOST_PROCESS_V2_BEGIN_NAMESPACE namespace environment { namespace detail { std::basic_string> get( basic_cstring_ref> key, error_code & ec) { std::basic_string> buf; std::size_t size = 0u; do { buf.resize(buf.size() + 4096); size = ::GetEnvironmentVariableW(key.c_str(), &buf.front(), static_cast(buf.size())); } while (size == buf.size()); buf.resize(size); if (buf.size() == 0) ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(); return buf; } void set(basic_cstring_ref> key, basic_cstring_ref> value, error_code & ec) { if (!::SetEnvironmentVariableW(key.c_str(), value.c_str())) ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(); } void unset(basic_cstring_ref> key, error_code & ec) { if (!::SetEnvironmentVariableW(key.c_str(), nullptr)) ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(); } std::basic_string> get( basic_cstring_ref> key, error_code & ec) { std::basic_string> buf; std::size_t size = 0u; do { buf.resize(buf.size() + 4096); size = ::GetEnvironmentVariableA(key.c_str(), &buf.front(), static_cast(buf.size())); } while (size == buf.size()); buf.resize(size); if (buf.size() == 0) ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(); return buf; } void set(basic_cstring_ref> key, basic_cstring_ref> value, error_code & ec) { if (!::SetEnvironmentVariableA(key.c_str(), value.c_str())) ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(); } void unset(basic_cstring_ref> key, error_code & ec) { if (!::SetEnvironmentVariableA(key.c_str(), nullptr)) ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(); } native_handle_type load_native_handle() { return ::GetEnvironmentStringsW(); } void native_handle_deleter::operator()(native_handle_type nh) const { ::FreeEnvironmentStringsW(nh); } native_iterator next(native_iterator nh) { while (*nh != L'\0') nh++; return ++nh; } native_iterator find_end(native_handle_type nh) { while ((*nh != L'\0') || (*std::next(nh) != L'\0')) nh++; return ++nh; } bool is_executable(const filesystem::path & pth, error_code & ec) { return filesystem::is_regular_file(pth, ec) && SHGetFileInfoW(pth.native().c_str(), 0,0,0, SHGFI_EXETYPE); } } } BOOST_PROCESS_V2_END_NAMESPACE #endif //BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP