master
h 74 lines 1.85 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 SubProcess.h
8
9 Abstract:
10
11 This file contains the SubProcess helper class definition.
12
13 --*/
14
15 #pragma once
16
17 #include <wil/resource.h>
18 #include <string>
19
20 namespace wsl::windows::common {
21
22 class SubProcess
23 {
24 public:
25 struct ProcessOutput
26 {
27 DWORD ExitCode{};
28 std::wstring Stdout;
29 std::wstring Stderr;
30 };
31
32 SubProcess(LPCWSTR ApplicationName, LPCWSTR CommandLine, DWORD Flags = CREATE_UNICODE_ENVIRONMENT, DWORD StartupFlags = STARTF_FORCEOFFFEEDBACK);
33
34 void SetStdHandles(HANDLE Stdin, HANDLE Stdout, HANDLE Stderr);
35 void SetPseudoConsole(HPCON Console);
36 void SetDesktopAppPolicy(DWORD Policy);
37 void InheritHandle(HANDLE Handle);
38 void SetEnvironment(LPVOID Environment);
39 void SetWorkingDirectory(LPCWSTR Directory);
40 void SetToken(HANDLE Token);
41 void SetShowWindow(WORD Show);
42 void SetFlags(DWORD Flag);
43 void SetJobObject(HANDLE JobObject);
44
45 wil::unique_handle Start();
46 DWORD Run(DWORD Timeout = INFINITE);
47
48 ProcessOutput RunAndCaptureOutput(DWORD Timeout = INFINITE, HANDLE StdErr = nullptr);
49
50 static DWORD GetExitCode(HANDLE Process, DWORD Timeout = INFINITE);
51
52 private:
53 helpers::unique_proc_attribute_list BuildProcessAttributes();
54
55 LPCWSTR m_applicationName = nullptr;
56 std::wstring m_commandLine;
57 LPVOID m_environment = nullptr;
58 LPCWSTR m_workingDirectory = nullptr;
59 LPCWSTR m_desktop = nullptr;
60 HANDLE m_token = nullptr;
61 DWORD m_flags = 0;
62 DWORD m_startupFlags = 0;
63
64 HANDLE m_stdIn = nullptr;
65 HANDLE m_stdOut = nullptr;
66 HANDLE m_stdErr = nullptr;
67 HPCON m_pseudoConsole = nullptr;
68 HANDLE m_jobObject = nullptr;
69 std::optional<DWORD> m_desktopAppPolicy;
70 std::optional<WORD> m_showWindow;
71 std::vector<HANDLE> m_inheritHandles;
72 };
73
74 } // namespace wsl::windows::common