null_out.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_NULL_OUT_HPP
  10. #define BOOST_PROCESS_WINDOWS_INITIALIZERS_NULL_OUT_HPP
  11. #include <boost/winapi/process.hpp>
  12. #include <boost/winapi/handles.hpp>
  13. #include <boost/winapi/handle_info.hpp>
  14. #include <boost/process/detail/handler_base.hpp>
  15. #include <boost/process/detail/used_handles.hpp>
  16. #include <boost/process/detail/windows/file_descriptor.hpp>
  17. namespace boost { namespace process { namespace detail { namespace windows {
  18. template<int p1, int p2>
  19. struct null_out : public ::boost::process::detail::handler_base, ::boost::process::detail::uses_handles
  20. {
  21. file_descriptor sink {"NUL", file_descriptor::write}; //works because it gets destroyed AFTER launch.
  22. ::boost::winapi::HANDLE_ get_used_handles() const { return sink.handle(); }
  23. template <typename WindowsExecutor>
  24. void on_setup(WindowsExecutor &e) const;
  25. };
  26. template<>
  27. template<typename WindowsExecutor>
  28. void null_out<1,-1>::on_setup(WindowsExecutor &e) const
  29. {
  30. boost::winapi::SetHandleInformation(sink.handle(),
  31. boost::winapi::HANDLE_FLAG_INHERIT_,
  32. boost::winapi::HANDLE_FLAG_INHERIT_);
  33. e.startup_info.hStdOutput = sink.handle();
  34. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  35. e.inherit_handles = true;
  36. }
  37. template<>
  38. template<typename WindowsExecutor>
  39. void null_out<2,-1>::on_setup(WindowsExecutor &e) const
  40. {
  41. boost::winapi::SetHandleInformation(sink.handle(),
  42. boost::winapi::HANDLE_FLAG_INHERIT_,
  43. boost::winapi::HANDLE_FLAG_INHERIT_);
  44. e.startup_info.hStdError = sink.handle();
  45. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  46. e.inherit_handles = true;
  47. }
  48. template<>
  49. template<typename WindowsExecutor>
  50. void null_out<1,2>::on_setup(WindowsExecutor &e) const
  51. {
  52. boost::winapi::SetHandleInformation(sink.handle(),
  53. boost::winapi::HANDLE_FLAG_INHERIT_,
  54. boost::winapi::HANDLE_FLAG_INHERIT_);
  55. e.startup_info.hStdOutput = sink.handle();
  56. e.startup_info.hStdError = sink.handle();
  57. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  58. e.inherit_handles = true;
  59. }
  60. }}}}
  61. #endif