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
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)
10 #ifndef BOOST_PROCESS_WINDOWS_CHILD_HPP
11 #define BOOST_PROCESS_WINDOWS_CHILD_HPP
13 #include <boost/move/move.hpp>
14 #include <boost/winapi/handles.hpp>
15 #include <boost/winapi/process.hpp>
16 #include <boost/winapi/jobs.hpp>
18 namespace boost { namespace process { namespace detail { namespace windows {
20 typedef ::boost::winapi::DWORD_ pid_t;
24 ::boost::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
26 explicit child_handle(const ::boost::winapi::PROCESS_INFORMATION_ &pi) :
30 explicit child_handle(pid_t pid) :
31 proc_info{nullptr, nullptr, 0,0}
33 auto h = ::boost::winapi::OpenProcess(
34 ::boost::winapi::PROCESS_ALL_ACCESS_,
35 static_cast<::boost::winapi::BOOL_>(0),
39 throw_last_error("OpenProcess() failed");
40 proc_info.hProcess = h;
41 proc_info.dwProcessId = pid;
44 child_handle() = default;
47 ::boost::winapi::CloseHandle(proc_info.hProcess);
48 ::boost::winapi::CloseHandle(proc_info.hThread);
50 child_handle(const child_handle & c) = delete;
51 child_handle(child_handle && c) : proc_info(c.proc_info)
53 c.proc_info.hProcess = ::boost::winapi::invalid_handle_value;
54 c.proc_info.hThread = ::boost::winapi::invalid_handle_value;
56 child_handle &operator=(const child_handle & c) = delete;
57 child_handle &operator=(child_handle && c)
59 ::boost::winapi::CloseHandle(proc_info.hProcess);
60 ::boost::winapi::CloseHandle(proc_info.hThread);
61 proc_info = c.proc_info;
62 c.proc_info.hProcess = ::boost::winapi::invalid_handle_value;
63 c.proc_info.hThread = ::boost::winapi::invalid_handle_value;
69 return static_cast<pid_t>(proc_info.dwProcessId);
72 typedef ::boost::winapi::HANDLE_ process_handle_t;
73 process_handle_t process_handle() const { return proc_info.hProcess; }
77 return (proc_info.hProcess != nullptr) &&
78 (proc_info.hProcess != ::boost::winapi::INVALID_HANDLE_VALUE_);
82 ::boost::winapi::BOOL_ value;
83 if (!::boost::winapi::IsProcessInJob(proc_info.hProcess, nullptr, &value))
84 throw_last_error("IsProcessinJob Failed");
87 bool in_group(std::error_code &ec) const noexcept
89 ::boost::winapi::BOOL_ value;
90 if (!::boost::winapi::IsProcessInJob(proc_info.hProcess, nullptr, &value))
91 ec = get_last_error();