本文整理汇总了C#中Microsoft.Win32.SafeHandles.SafeProcessHandle.Close方法的典型用法代码示例。如果您正苦于以下问题:C# SafeProcessHandle.Close方法的具体用法?C# SafeProcessHandle.Close怎么用?C# SafeProcessHandle.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Win32.SafeHandles.SafeProcessHandle
的用法示例。
在下文中一共展示了SafeProcessHandle.Close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecWaitWithCaptureUnimpersonated
//.........这里部分代码省略.........
procSH.InitialSetHandle(pi.hProcess);
if ( pi.hThread != (IntPtr)0 && pi.hThread != (IntPtr)NativeMethods.INVALID_HANDLE_VALUE)
threadSH.InitialSetHandle(pi.hThread);
}
}
else {
#if FEATURE_PAL
throw new NotSupportedException();
#else
success = SafeUserTokenHandle.DuplicateTokenEx(
userToken,
NativeMethods.TOKEN_ALL_ACCESS,
null,
NativeMethods.IMPERSONATION_LEVEL_SecurityImpersonation,
NativeMethods.TOKEN_TYPE_TokenPrimary,
out primaryToken
);
if (success) {
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
success = NativeMethods.CreateProcessAsUser(
primaryToken, // int token,
null, // String lpApplicationName,
cmd, // String lpCommandLine,
null, // SECURITY_ATTRIBUTES lpProcessAttributes,
null, // SECURITY_ATTRIBUTES lpThreadAttributes,
true, // bool bInheritHandles,
0, // int dwCreationFlags,
new HandleRef(null, environmentPtr), // IntPtr lpEnvironment,
currentDir, // String lpCurrentDirectory,
si, // STARTUPINFO lpStartupInfo,
pi); // PROCESS_INFORMATION lpProcessInformation);
if ( pi.hProcess!= (IntPtr)0 && pi.hProcess!= (IntPtr)NativeMethods.INVALID_HANDLE_VALUE)
procSH.InitialSetHandle(pi.hProcess);
if ( pi.hThread != (IntPtr)0 && pi.hThread != (IntPtr)NativeMethods.INVALID_HANDLE_VALUE)
threadSH.InitialSetHandle(pi.hThread);
}
}
#endif // !FEATURE_PAL
}
}
}
finally {
// Close the file handles
if (!success && (primaryToken != null && !primaryToken.IsInvalid)) {
primaryToken.Close();
primaryToken = null;
}
output.Close();
error.Close();
}
if (success) {
try {
bool signaled;
ProcessWaitHandle pwh = null;
try {
pwh = new ProcessWaitHandle(procSH);
signaled = pwh.WaitOne(ProcessTimeOut, false);
} finally {
if (pwh != null)
pwh.Close();
}
// Check for timeout
if (!signaled) {
throw new ExternalException(SR.GetString(SR.ExecTimeout, cmd), NativeMethods.WAIT_TIMEOUT);
}
// Check the process's exit code
int status = NativeMethods.STILL_ACTIVE;
if (!NativeMethods.GetExitCodeProcess(procSH, out status)) {
throw new ExternalException(SR.GetString(SR.ExecCantGetRetCode, cmd), Marshal.GetLastWin32Error());
}
retValue = status;
}
finally {
procSH.Close();
threadSH.Close();
if (primaryToken != null && !primaryToken.IsInvalid)
primaryToken.Close();
}
}
else {
int err = Marshal.GetLastWin32Error();
if (err == NativeMethods.ERROR_NOT_ENOUGH_MEMORY)
throw new OutOfMemoryException();
Win32Exception win32Exception = new Win32Exception(err);
ExternalException ex = new ExternalException(SR.GetString(SR.ExecCantExec, cmd), win32Exception);
throw ex;
}
return retValue;
}
示例2: ExecWaitWithCaptureUnimpersonated
private static unsafe int ExecWaitWithCaptureUnimpersonated(SafeUserTokenHandle userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName, string trueCmdLine)
{
IntSecurity.UnmanagedCode.Demand();
if ((outputName == null) || (outputName.Length == 0))
{
outputName = tempFiles.AddExtension("out");
}
if ((errorName == null) || (errorName.Length == 0))
{
errorName = tempFiles.AddExtension("err");
}
FileStream stream = CreateInheritedFile(outputName);
FileStream stream2 = CreateInheritedFile(errorName);
bool flag = false;
Microsoft.Win32.SafeNativeMethods.PROCESS_INFORMATION lpProcessInformation = new Microsoft.Win32.SafeNativeMethods.PROCESS_INFORMATION();
Microsoft.Win32.SafeHandles.SafeProcessHandle processHandle = new Microsoft.Win32.SafeHandles.SafeProcessHandle();
Microsoft.Win32.SafeHandles.SafeThreadHandle handle2 = new Microsoft.Win32.SafeHandles.SafeThreadHandle();
SafeUserTokenHandle hNewToken = null;
try
{
Microsoft.Win32.NativeMethods.STARTUPINFO startupinfo;
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
writer.Write(currentDir);
writer.Write("> ");
writer.WriteLine((trueCmdLine != null) ? trueCmdLine : cmd);
writer.WriteLine();
writer.WriteLine();
writer.Flush();
startupinfo = new Microsoft.Win32.NativeMethods.STARTUPINFO {
cb = Marshal.SizeOf(startupinfo),
dwFlags = 0x101,
wShowWindow = 0,
hStdOutput = stream.SafeFileHandle,
hStdError = stream2.SafeFileHandle,
hStdInput = new SafeFileHandle(Microsoft.Win32.UnsafeNativeMethods.GetStdHandle(-10), false)
};
StringDictionary sd = new StringDictionary();
foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
{
sd[(string) entry.Key] = (string) entry.Value;
}
sd["_ClrRestrictSecAttributes"] = "1";
byte[] buffer = EnvironmentBlock.ToByteArray(sd, false);
try
{
fixed (byte* numRef = buffer)
{
IntPtr lpEnvironment = new IntPtr((void*) numRef);
if ((userToken == null) || userToken.IsInvalid)
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{
goto Label_0325;
}
finally
{
flag = Microsoft.Win32.NativeMethods.CreateProcess(null, new StringBuilder(cmd), null, null, true, 0, lpEnvironment, currentDir, startupinfo, lpProcessInformation);
if ((lpProcessInformation.hProcess != IntPtr.Zero) && (lpProcessInformation.hProcess != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE))
{
processHandle.InitialSetHandle(lpProcessInformation.hProcess);
}
if ((lpProcessInformation.hThread != IntPtr.Zero) && (lpProcessInformation.hThread != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE))
{
handle2.InitialSetHandle(lpProcessInformation.hThread);
}
}
}
flag = SafeUserTokenHandle.DuplicateTokenEx(userToken, 0xf01ff, null, 2, 1, out hNewToken);
if (flag)
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{
}
finally
{
flag = Microsoft.Win32.NativeMethods.CreateProcessAsUser(hNewToken, null, cmd, null, null, true, 0, new HandleRef(null, lpEnvironment), currentDir, startupinfo, lpProcessInformation);
if ((lpProcessInformation.hProcess != IntPtr.Zero) && (lpProcessInformation.hProcess != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE))
{
processHandle.InitialSetHandle(lpProcessInformation.hProcess);
}
if ((lpProcessInformation.hThread != IntPtr.Zero) && (lpProcessInformation.hThread != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE))
{
handle2.InitialSetHandle(lpProcessInformation.hThread);
}
}
}
}
}
finally
{
numRef = null;
}
}
finally
{
if ((!flag && (hNewToken != null)) && !hNewToken.IsInvalid)
{
hNewToken.Close();
//.........这里部分代码省略.........