env_init.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_ENV_INIT_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_ENV_INIT_HPP_
  7. #include <boost/winapi/error_codes.hpp>
  8. #include <boost/winapi/process.hpp>
  9. #include <boost/process/detail/config.hpp>
  10. #include <boost/process/detail/handler_base.hpp>
  11. #include <boost/process/environment.hpp>
  12. namespace boost { namespace process { namespace detail { namespace windows {
  13. template<typename Char>
  14. struct env_init : public ::boost::process::detail::handler_base
  15. {
  16. boost::process::basic_environment<Char> env;
  17. env_init(boost::process::basic_environment<Char> && env) : env(std::move(env)) {};
  18. env_init(const boost::process::basic_environment<Char> & env) : env(env) {};
  19. constexpr static ::boost::winapi::DWORD_ creation_flag(char) {return 0u;}
  20. constexpr static ::boost::winapi::DWORD_ creation_flag(wchar_t)
  21. {
  22. return ::boost::winapi::CREATE_UNICODE_ENVIRONMENT_;
  23. }
  24. template <class WindowsExecutor>
  25. void on_setup(WindowsExecutor &exec) const
  26. {
  27. auto e = env.native_handle();
  28. if (*e == null_char<char>())
  29. {
  30. exec.set_error(std::error_code(::boost::winapi::ERROR_BAD_ENVIRONMENT_, std::system_category()),
  31. "Empty Environment");
  32. }
  33. exec.env = e;
  34. exec.creation_flags |= creation_flag(Char());
  35. }
  36. };
  37. }}}}
  38. #endif /* BOOST_PROCESS_DETAIL_WINDOWS_ENV_INIT_HPP_ */