2 using Microsoft.Win32.SafeHandles;
20 private const int ProcessTimeOut = 600000;
22 internal static string GetRuntimeInstallDirectory()
27 private static FileStream CreateInheritedFile(
string file)
37 string outputName =
null;
38 string errorName =
null;
62 return ExecWaitWithCapture(
null, cmd, currentDir, tempFiles, ref outputName, ref errorName,
null);
90 internal static int ExecWaitWithCapture(SafeUserTokenHandle userToken,
string cmd,
string currentDir,
TempFileCollection tempFiles, ref
string outputName, ref
string errorName,
string trueCmdLine)
98 return ExecWaitWithCaptureUnimpersonated(userToken, cmd, currentDir, tempFiles, ref outputName, ref errorName, trueCmdLine);
102 ReImpersonate(impersonation);
111 private unsafe
static int ExecWaitWithCaptureUnimpersonated(SafeUserTokenHandle userToken,
string cmd,
string currentDir, TempFileCollection tempFiles, ref
string outputName, ref
string errorName,
string trueCmdLine)
113 IntSecurity.UnmanagedCode.Demand();
115 if (outputName ==
null || outputName.Length == 0)
117 outputName = tempFiles.AddExtension(
"out");
119 if (errorName ==
null || errorName.Length == 0)
121 errorName = tempFiles.AddExtension(
"err");
123 FileStream fileStream = CreateInheritedFile(outputName);
124 FileStream fileStream2 = CreateInheritedFile(errorName);
126 Microsoft.Win32.SafeNativeMethods.PROCESS_INFORMATION pROCESS_INFORMATION =
new Microsoft.Win32.SafeNativeMethods.PROCESS_INFORMATION();
127 SafeProcessHandle safeProcessHandle =
new SafeProcessHandle();
128 Microsoft.Win32.SafeHandles.SafeThreadHandle safeThreadHandle =
new Microsoft.Win32.SafeHandles.SafeThreadHandle();
129 SafeUserTokenHandle hNewToken =
null;
133 streamWriter.
Write(currentDir);
134 streamWriter.
Write(
"> ");
135 streamWriter.
WriteLine((trueCmdLine !=
null) ? trueCmdLine : cmd);
138 streamWriter.
Flush();
139 Microsoft.Win32.NativeMethods.STARTUPINFO sTARTUPINFO =
new Microsoft.Win32.NativeMethods.STARTUPINFO();
141 sTARTUPINFO.dwFlags = 257;
142 sTARTUPINFO.wShowWindow = 0;
145 sTARTUPINFO.hStdInput =
new SafeFileHandle(Microsoft.Win32.UnsafeNativeMethods.GetStdHandle(-10), ownsHandle:
false);
147 foreach (
DictionaryEntry environmentVariable
in Environment.GetEnvironmentVariables())
149 stringDictionary[(string)environmentVariable.
Key] = (
string)environmentVariable.
Value;
151 stringDictionary[
"_ClrRestrictSecAttributes"] =
"1";
152 byte[] array = EnvironmentBlock.ToByteArray(stringDictionary, unicode:
false);
155 byte[] array2 = array;
156 fixed (
byte* value = array2)
158 IntPtr intPtr =
new IntPtr(value);
159 if (userToken ==
null || userToken.IsInvalid)
167 flag = Microsoft.Win32.NativeMethods.CreateProcess(
null,
new StringBuilder(cmd),
null,
null, bInheritHandles:
true, 0, intPtr, currentDir, sTARTUPINFO, pROCESS_INFORMATION);
168 if (pROCESS_INFORMATION.hProcess != (IntPtr)0 && pROCESS_INFORMATION.hProcess != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
170 safeProcessHandle.InitialSetHandle(pROCESS_INFORMATION.hProcess);
172 if (pROCESS_INFORMATION.hThread != (IntPtr)0 && pROCESS_INFORMATION.hThread != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
174 safeThreadHandle.InitialSetHandle(pROCESS_INFORMATION.hThread);
180 flag = SafeUserTokenHandle.DuplicateTokenEx(userToken, 983551,
null, 2, 1, out hNewToken);
189 flag = Microsoft.Win32.NativeMethods.CreateProcessAsUser(hNewToken,
null, cmd,
null,
null, bInheritHandles:
true, 0,
new HandleRef(
null, intPtr), currentDir, sTARTUPINFO, pROCESS_INFORMATION);
190 if (pROCESS_INFORMATION.hProcess != (IntPtr)0 && pROCESS_INFORMATION.hProcess != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
192 safeProcessHandle.InitialSetHandle(pROCESS_INFORMATION.hProcess);
194 if (pROCESS_INFORMATION.hThread != (IntPtr)0 && pROCESS_INFORMATION.hThread != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
196 safeThreadHandle.InitialSetHandle(pROCESS_INFORMATION.hThread);
209 if (!flag && hNewToken !=
null && !hNewToken.IsInvalid)
221 ProcessWaitHandle processWaitHandle =
null;
225 processWaitHandle =
new ProcessWaitHandle(safeProcessHandle);
226 flag2 = processWaitHandle.WaitOne(600000, exitContext:
false);
230 processWaitHandle?.Close();
237 if (!Microsoft.Win32.NativeMethods.GetExitCodeProcess(safeProcessHandle, out exitCode))
245 safeProcessHandle.Close();
246 safeThreadHandle.Close();
247 if (hNewToken !=
null && !hNewToken.IsInvalid)
254 if (lastWin32Error == 8)
256 throw new OutOfMemoryException();
272 impersonation.
Undo();
Represents a character encoding.To browse the .NET Framework source code for this type,...
Describes a set of security permissions applied to code. This class cannot be inherited.
Provides command execution functions for invoking compilers. This class cannot be inherited.
static int ExecWaitWithCapture(IntPtr userToken, string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
Executes the specified command using the specified user token and temporary files,...
virtual WindowsImpersonationContext Impersonate()
Impersonates the user represented by the T:System.Security.Principal.WindowsIdentity object.
FileMode
Specifies how the operating system should open a file.
Implements a hash table with the key and the value strongly typed to be strings rather than objects.
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
static int ExecWaitWithCapture(string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
Executes the specified command using the specified current directory and temporary files,...
Wraps a managed object holding a handle to a resource that is passed to unmanaged code using platform...
Provides a collection of static methods that return information about the common language runtime env...
static string GetRuntimeDirectory()
Returns the directory where the common language runtime is installed.
void Undo()
Reverts the user context to the Windows user represented by this object.
Represents a Windows user.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Implements a T:System.IO.TextWriter for writing characters to a stream in a particular encoding....
Provides information about, and means to manipulate, the current environment and platform....
virtual void Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated wi...
Throws an exception for a Win32 error code.
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
A platform-specific type that is used to represent a pointer or a handle.
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
override void Write(char value)
Writes a character to the stream.
static int ExecWaitWithCapture(string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
Executes the specified command using the specified temporary files and waits for the call to return,...
The base exception type for all COM interop exceptions and structured exception handling (SEH) except...
virtual SafeFileHandle SafeFileHandle
Gets a T:Microsoft.Win32.SafeHandles.SafeFileHandle object that represents the operating system file ...
FileAccess
Defines constants for read, write, or read/write access to a file.
object Value
Gets or sets the value in the key/value pair.
static void ExecWait(string cmd, TempFileCollection tempFiles)
Executes the command using the specified temporary files and waits for the call to return.
virtual void WriteLine()
Writes a line terminator to the text string or stream.
static Encoding UTF8
Gets an encoding for the UTF-8 format.
static int ExecWaitWithCapture(IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
Executes the specified command using the specified user token, current directory, and temporary files...
object Key
Gets or sets the key in the key/value pair.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
static string CurrentDirectory
Gets or sets the fully qualified path of the current working directory.
Defines a dictionary key/value pair that can be set or retrieved.
override void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
Represents a collection of temporary files.
Represents the Windows user prior to an impersonation operation.
FileShare
Contains constants for controlling the kind of access other T:System.IO.FileStream objects can have t...
Provides a set of static methods and properties that provide support for compilers....