-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessWin32.cs
169 lines (138 loc) · 4.63 KB
/
ProcessWin32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace CreatorDK.Diagnostics.Win32
{
public partial class ProcessWin32
{
string _applicationName;
string _commandLine;
SECURITY_ATTRIBUTES? _processSecurityAttributes;
SECURITY_ATTRIBUTES? _threadSecurityAttributes;
bool _inheritHandles = false;
uint _creationFlags = 0;
string _environment;
string _currentDirectory;
readonly STARTUPINFO _startupInfo = new STARTUPINFO();
PROCESS_INFORMATION _processInformation = new PROCESS_INFORMATION();
int _processId = 0;
int _threadId = 0;
SafeHandle _processHandle;
IntPtr _threadHandle;
public string ApplicationName
{
get => _applicationName;
set => _applicationName = value;
}
public string CommandLine
{
get => _commandLine;
set => _commandLine = value;
}
public SECURITY_ATTRIBUTES? ProcessSecurityAttributes
{
get => _processSecurityAttributes;
set => _processSecurityAttributes = value;
}
public SECURITY_ATTRIBUTES? ThreadSecurityAttributes
{
get => _threadSecurityAttributes;
set => _threadSecurityAttributes = value;
}
public bool InheritHandles
{
get => _inheritHandles;
set => _inheritHandles = value;
}
public uint CreationFlags
{
get => _creationFlags;
set => _creationFlags = value;
}
public string Environment
{
get => _environment;
set => _environment = value;
}
public string CurrentDirectory
{
get => _currentDirectory;
set => _currentDirectory = value;
}
public STARTUPINFO StartupInfo
{
get => _startupInfo;
}
public int Id
{
get => _processId;
}
public int ThreadId
{
get => _threadId;
}
public SafeHandle ProcessSafeHandle
{
get => _processHandle;
}
public IntPtr? ThreadDangerousHandle
{
get => _threadHandle;
}
public ProcessWin32() { }
public ProcessWin32(STARTUPINFO startupInfo)
{
_startupInfo = startupInfo;
}
public ProcessWin32(string applicationName, string commandLine = null)
{
_applicationName = applicationName;
_commandLine = commandLine;
}
public bool Start ()
{
#pragma warning disable CS8604 //Marshalling dont support nullable types
#pragma warning disable CS8600
//#pragma warning disable CS8629
SECURITY_ATTRIBUTES processSecurityAttributes = _processSecurityAttributes == null ? new SECURITY_ATTRIBUTES() : (SECURITY_ATTRIBUTES)_processSecurityAttributes;
SECURITY_ATTRIBUTES threadSecurityAttributes = _threadSecurityAttributes == null ? new SECURITY_ATTRIBUTES() : (SECURITY_ATTRIBUTES)_threadSecurityAttributes;
StringBuilder commandLine = null;
if (_commandLine != null)
commandLine = new StringBuilder(_commandLine);
StringBuilder environment = null;
if (_environment != null)
environment = new StringBuilder(_environment);
PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION();
bool result = NativeMethods.Process.CreateProcess(
_applicationName,
commandLine,
processSecurityAttributes,
threadSecurityAttributes,
_inheritHandles,
_creationFlags,
environment,
_currentDirectory,
_startupInfo,
processInformation
);
#pragma warning restore CS8604
#pragma warning restore CS6800
//#pragma warning restore CS8629
_processInformation = processInformation;
if (_processInformation != null)
{
_processId = _processInformation.dwProcessId;
_threadId = _processInformation.dwThreadId;
_processHandle = new SafeProcessHandle(_processInformation.hProcess, true);
_threadHandle = _processInformation.hThread;
}
return result;
}
public Process GetProcess()
{
return Process.GetProcessById(_processId);
}
}
}