Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:11

0001 //
0002 // boost/process/v2/windows/default_launcher.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_PROCESS_V2_WINDOWS_WITH_LOGON_LAUNCHER_HPP
0012 #define BOOST_PROCESS_V2_WINDOWS_WITH_LOGON_LAUNCHER_HPP
0013 
0014 #include <boost/process/v2/windows/default_launcher.hpp>
0015 
0016 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0017 namespace windows
0018 {
0019 
0020 /// A windows launcher using CreateProcessWithLogon instead of CreateProcess
0021 struct with_logon_launcher : default_launcher
0022 {
0023   std::wstring username, password, domain;
0024   DWORD logon_flags{0u};
0025 
0026   with_logon_launcher(std::wstring username = L"",
0027                       std::wstring password = L"",
0028                       std::wstring domain = L"",
0029                       DWORD logon_flags = 0u) :
0030                       username(std::move(username)),
0031                       password(std::move(password)),
0032                       domain(std::move(domain)),
0033                       logon_flags(logon_flags)
0034   {
0035   }
0036 
0037 
0038   template<typename ExecutionContext, typename Args, typename ... Inits>
0039   auto operator()(ExecutionContext & context,
0040                   error_code & ec,
0041                   const typename std::enable_if<std::is_convertible<
0042                              ExecutionContext&, BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
0043                              filesystem::path >::type & executable,
0044                   Args && args,
0045                   Inits && ... inits ) -> basic_process<typename ExecutionContext::executor_type>
0046   {
0047       return (*this)(context.get_executor(), ec, executable, std::forward<Args>(args), std::forward<Inits>(inits)...);
0048   }
0049 
0050 
0051   template<typename ExecutionContext, typename Args, typename ... Inits>
0052   auto operator()(ExecutionContext & context,
0053                      const typename std::enable_if<std::is_convertible<
0054                              ExecutionContext&, BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
0055                              filesystem::path >::type & executable,
0056                      Args && args,
0057                      Inits && ... inits ) -> basic_process<typename ExecutionContext::executor_type>
0058   {
0059       return (*this)(context.get_executor(), executable, std::forward<Args>(args), std::forward<Inits>(inits)...);
0060   }
0061 
0062   template<typename Executor, typename Args, typename ... Inits>
0063   auto operator()(Executor exec,
0064                      const typename std::enable_if<
0065                              BOOST_PROCESS_V2_ASIO_NAMESPACE::execution::is_executor<Executor>::value ||
0066                              BOOST_PROCESS_V2_ASIO_NAMESPACE::is_executor<Executor>::value,
0067                              filesystem::path >::type & executable,
0068                      Args && args,
0069                      Inits && ... inits ) -> basic_process<Executor>
0070   {
0071       error_code ec;
0072       auto proc =  (*this)(std::move(exec), ec, executable, std::forward<Args>(args), std::forward<Inits>(inits)...);
0073 
0074       if (ec)
0075           v2::detail::throw_error(ec, "with_logon_launcher");
0076 
0077       return proc;
0078   }
0079   
0080   template<typename Executor, typename Args, typename ... Inits>
0081   auto operator()(Executor exec,
0082                      error_code & ec,
0083                      const typename std::enable_if<
0084                              BOOST_PROCESS_V2_ASIO_NAMESPACE::execution::is_executor<Executor>::value || 
0085                              BOOST_PROCESS_V2_ASIO_NAMESPACE::is_executor<Executor>::value,
0086                              filesystem::path >::type & executable,
0087                      Args && args,
0088                      Inits && ... inits ) -> basic_process<Executor>
0089   {
0090     auto command_line = this->build_command_line(executable, args);
0091 
0092     ec = detail::on_setup(*this, executable, command_line, inits...);
0093     if (ec)
0094     {
0095       detail::on_error(*this, executable, command_line, ec, inits...);
0096       return basic_process<Executor>(exec);
0097     }
0098     auto ok = ::CreateProcessWithLogonW(
0099         username.c_str(),
0100         domain.empty() ? nullptr : domain.c_str(),
0101         password.c_str(),
0102         logon_flags,
0103         executable.empty() ? nullptr : executable.c_str(),
0104         command_line.empty() ? nullptr : &command_line.front(),
0105         creation_flags,
0106         environment,
0107         current_directory.empty() ? nullptr : current_directory.c_str(),
0108         &startup_info.StartupInfo,
0109         &process_information);
0110 
0111 
0112     if (ok == 0)
0113     {
0114       BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0115       detail::on_error(*this, executable, command_line, ec, inits...);
0116 
0117       if (process_information.hProcess != INVALID_HANDLE_VALUE)
0118         ::CloseHandle(process_information.hProcess);
0119       if (process_information.hThread != INVALID_HANDLE_VALUE)
0120         ::CloseHandle(process_information.hThread);
0121 
0122       return basic_process<Executor>(exec);
0123     } else
0124     {
0125       detail::on_success(*this, executable, command_line, inits...);
0126 
0127       if (process_information.hThread != INVALID_HANDLE_VALUE)
0128         ::CloseHandle(process_information.hThread);
0129 
0130       return basic_process<Executor>(exec,
0131                                      this->process_information.dwProcessId,
0132                                      this->process_information.hProcess);
0133     }
0134   }
0135 };
0136 
0137 }
0138 BOOST_PROCESS_V2_END_NAMESPACE
0139 
0140 #endif //  BOOST_PROCESS_V2_WINDOWS_WITH_LOGON_LAUNCHER_HPP