winapi_common
process.hpp
1 // Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
2 // This file is part of the "winapi-common" project.
3 // For details, see https://github.com/egor-tensin/winapi-common.
4 // Distributed under the MIT License.
5 
6 #pragma once
7 
8 #include "cmd_line.hpp"
9 #include "handle.hpp"
10 #include "process_io.hpp"
11 #include "resource.hpp"
12 
13 #include <boost/optional.hpp>
14 
15 #include <windows.h>
16 
17 #include <cstdint>
18 #include <string>
19 #include <utility>
20 
21 namespace winapi {
22 
25  enum ConsoleCreationMode {
26  ConsoleNone,
27  ConsoleInherit,
28  ConsoleNew,
29  };
30 
31  explicit ProcessParameters(const CommandLine& cmd_line) : cmd_line{cmd_line} {}
32 
33  CommandLine cmd_line;
34  boost::optional<process::IO> io;
35  ConsoleCreationMode console_mode = ConsoleNew;
36 };
37 
40  static ShellParameters runas(const CommandLine& cmd_line) {
41  ShellParameters params{cmd_line};
42  params.verb = "runas";
43  return params;
44  }
45 
46  explicit ShellParameters(const CommandLine& cmd_line) : ProcessParameters{cmd_line} {}
47 
48  boost::optional<std::string> verb;
49 };
50 
54 class Process {
55 public:
56  using ID = DWORD;
57 
61  static Process create(const CommandLine&);
63  static Process create(const CommandLine&, process::IO);
64 
66  static Process shell(const ShellParameters&);
68  static Process shell(const CommandLine&);
69 
71  static Process current();
77  static Process open(ID id, DWORD permissions = default_permissions());
79  static Process open_r(ID);
80 
82  static DWORD default_permissions();
84  static DWORD read_permissions();
85 
87  ID get_id() const { return m_id; }
89  const Handle& get_handle() const { return m_handle; }
90 
92  bool is_running() const;
94  void wait() const;
96  void terminate(int ec = 0) const;
98  void shut_down(int ec = 0) const;
100  int get_exit_code() const;
101 
103  std::string get_exe_path() const;
104 
106  static Resource get_resource(uint32_t id);
111  static std::string get_resource_string(uint32_t id);
112 
113 private:
114  explicit Process(Handle&& handle);
115  Process(ID, Handle&& handle);
116 
117  static HMODULE get_exe_module();
118 
119  ID m_id;
120  Handle m_handle;
121 };
122 
123 } // namespace winapi
Command line for the current process or for launching new processes.
Definition: cmd_line.hpp:21
HANDLE wrapper.
Definition: handle.hpp:25
Create a new process or open an existing process.
Definition: process.hpp:54
void terminate(int ec=0) const
Definition: process.cpp:315
void shut_down(int ec=0) const
Definition: process.cpp:321
const Handle & get_handle() const
Definition: process.hpp:89
void wait() const
Definition: process.cpp:301
int get_exit_code() const
Definition: process.cpp:326
static Process open(ID id, DWORD permissions=default_permissions())
Definition: process.cpp:269
static Resource get_resource(uint32_t id)
Definition: process.cpp:370
static Process current()
Definition: process.cpp:265
static DWORD read_permissions()
Definition: process.cpp:281
static DWORD default_permissions()
Definition: process.cpp:277
bool is_running() const
Definition: process.cpp:285
static Process create(ProcessParameters)
Definition: process.cpp:241
std::string get_exe_path() const
Definition: process.cpp:342
ID get_id() const
Definition: process.hpp:87
static Process shell(const ShellParameters &)
Definition: process.cpp:256
static std::string get_resource_string(uint32_t id)
Definition: process.cpp:358
static Process open_r(ID)
Definition: process.cpp:273
Process parameters for Process::create().
Definition: process.hpp:24
Resources embedded in a PE (Portable Executable).
Definition: resource.hpp:15
Process parameters for Process::shell().
Definition: process.hpp:39
Child process IO settings.
Definition: process_io.hpp:61