本文整理汇总了C#中Microsoft.Win32.SafeHandles.SafeProcessHandle.InitialSetHandle方法的典型用法代码示例。如果您正苦于以下问题:C# SafeProcessHandle.InitialSetHandle方法的具体用法?C# SafeProcessHandle.InitialSetHandle怎么用?C# SafeProcessHandle.InitialSetHandle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Win32.SafeHandles.SafeProcessHandle
的用法示例。
在下文中一共展示了SafeProcessHandle.InitialSetHandle方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
FileStream output;
FileStream error;
int retValue = 0;
if (outputName == null || outputName.Length == 0)
outputName = tempFiles.AddExtension("out");
if (errorName == null || errorName.Length == 0)
errorName = tempFiles.AddExtension("err");
// Create the files
output = CreateInheritedFile(outputName);
error = CreateInheritedFile(errorName);
bool success = false;
SafeNativeMethods.PROCESS_INFORMATION pi = new SafeNativeMethods.PROCESS_INFORMATION();
SafeProcessHandle procSH = new SafeProcessHandle();
SafeThreadHandle threadSH = new SafeThreadHandle();
SafeUserTokenHandle primaryToken = null;
try {
// Output the command line...
StreamWriter sw = new StreamWriter(output, Encoding.UTF8);
sw.Write(currentDir);
sw.Write("> ");
// 'true' command line is used in case the command line points to
// a response file
sw.WriteLine(trueCmdLine != null ? trueCmdLine : cmd);
sw.WriteLine();
sw.WriteLine();
sw.Flush();
NativeMethods.STARTUPINFO si = new NativeMethods.STARTUPINFO();
si.cb = Marshal.SizeOf(si);
#if FEATURE_PAL
si.dwFlags = NativeMethods.STARTF_USESTDHANDLES;
#else //!FEATURE_PAL
si.dwFlags = NativeMethods.STARTF_USESTDHANDLES | NativeMethods.STARTF_USESHOWWINDOW;
si.wShowWindow = NativeMethods.SW_HIDE;
#endif //!FEATURE_PAL
si.hStdOutput = output.SafeFileHandle;
si.hStdError = error.SafeFileHandle;
si.hStdInput = new SafeFileHandle(UnsafeNativeMethods.GetStdHandle(NativeMethods.STD_INPUT_HANDLE), false);
//
// Prepare the environment
//
#if PLATFORM_UNIX
StringDictionary environment = new CaseSensitiveStringDictionary();
#else
StringDictionary environment = new StringDictionary ();
#endif // PLATFORM_UNIX
// Add the current environment
foreach ( DictionaryEntry entry in Environment.GetEnvironmentVariables () )
environment[(string) entry.Key] = (string) entry.Value;
// Add the flag to indicate restricted security in the process
environment["_ClrRestrictSecAttributes"] = "1";
#if DEBUG
environment["OANOCACHE"] = "1";
#endif
// set up the environment block parameter
byte[] environmentBytes = EnvironmentBlock.ToByteArray(environment, false);
fixed (byte* environmentBytesPtr = environmentBytes) {
IntPtr environmentPtr = new IntPtr((void*)environmentBytesPtr);
if (userToken == null || userToken.IsInvalid) {
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
success = NativeMethods.CreateProcess(
null, // String lpApplicationName,
new StringBuilder(cmd), // String lpCommandLine,
null, // SECURITY_ATTRIBUTES lpProcessAttributes,
null, // SECURITY_ATTRIBUTES lpThreadAttributes,
true, // bool bInheritHandles,
0, // int dwCreationFlags,
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);
}
}
else {
//.........这里部分代码省略.........
示例2: StartWithCreateProcess
//.........这里部分代码省略.........
workingDirectory = Environment.CurrentDirectory;
}
if (startInfo.UserName.Length != 0)
{
Microsoft.Win32.NativeMethods.LogonFlags logonFlags = 0;
if (startInfo.LoadUserProfile)
{
logonFlags = Microsoft.Win32.NativeMethods.LogonFlags.LOGON_WITH_PROFILE;
}
IntPtr password = IntPtr.Zero;
try
{
if (startInfo.Password == null)
{
password = Marshal.StringToCoTaskMemUni(string.Empty);
}
else
{
password = Marshal.SecureStringToCoTaskMemUnicode(startInfo.Password);
}
RuntimeHelpers.PrepareConstrainedRegions();
try
{
}
finally
{
flag = Microsoft.Win32.NativeMethods.CreateProcessWithLogonW(startInfo.UserName, startInfo.Domain, password, logonFlags, null, cmdLine, creationFlags, zero, workingDirectory, lpStartupInfo, lpProcessInformation);
if (!flag)
{
error = Marshal.GetLastWin32Error();
}
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);
}
}
if (!flag)
{
if ((error != 0xc1) && (error != 0xd8))
{
throw new Win32Exception(error);
}
throw new Win32Exception(error, SR.GetString("InvalidApplication"));
}
goto Label_03E0;
}
finally
{
if (password != IntPtr.Zero)
{
Marshal.ZeroFreeCoTaskMemUnicode(password);
}
}
}
RuntimeHelpers.PrepareConstrainedRegions();
try
{
}
finally
{
flag = Microsoft.Win32.NativeMethods.CreateProcess(null, cmdLine, null, null, true, creationFlags, zero, workingDirectory, lpStartupInfo, lpProcessInformation);
if (!flag)
示例3: StartCore
//.........这里部分代码省略.........
}
string workingDirectory = startInfo.WorkingDirectory;
if (workingDirectory == string.Empty)
workingDirectory = Directory.GetCurrentDirectory();
if (startInfo.UserName.Length != 0)
{
Interop.mincore.LogonFlags logonFlags = (Interop.mincore.LogonFlags)0;
if (startInfo.LoadUserProfile)
{
logonFlags = Interop.mincore.LogonFlags.LOGON_WITH_PROFILE;
}
try { }
finally
{
retVal = Interop.mincore.CreateProcessWithLogonW(
startInfo.UserName,
startInfo.Domain,
startInfo.PasswordInClearText,
logonFlags,
null, // we don't need this since all the info is in commandLine
commandLine,
creationFlags,
environmentPtr,
workingDirectory,
startupInfo, // pointer to STARTUPINFO
processInfo // pointer to PROCESS_INFORMATION
);
if (!retVal)
errorCode = Marshal.GetLastWin32Error();
if (processInfo.hProcess != IntPtr.Zero && processInfo.hProcess != (IntPtr)INVALID_HANDLE_VALUE)
procSH.InitialSetHandle(processInfo.hProcess);
if (processInfo.hThread != IntPtr.Zero && processInfo.hThread != (IntPtr)INVALID_HANDLE_VALUE)
threadSH.InitialSetHandle(processInfo.hThread);
}
if (!retVal)
{
if (errorCode == Interop.mincore.Errors.ERROR_BAD_EXE_FORMAT || errorCode == Interop.mincore.Errors.ERROR_EXE_MACHINE_TYPE_MISMATCH)
throw new Win32Exception(errorCode, SR.InvalidApplication);
throw new Win32Exception(errorCode);
}
}
else
{
try { }
finally
{
retVal = Interop.mincore.CreateProcess(
null, // we don't need this since all the info is in commandLine
commandLine, // pointer to the command line string
ref unused_SecAttrs, // address to process security attributes, we don't need to inheriat the handle
ref unused_SecAttrs, // address to thread security attributes.
true, // handle inheritance flag
creationFlags, // creation flags
environmentPtr, // pointer to new environment block
workingDirectory, // pointer to current directory name
startupInfo, // pointer to STARTUPINFO
processInfo // pointer to PROCESS_INFORMATION
);
if (!retVal)
errorCode = Marshal.GetLastWin32Error();
if (processInfo.hProcess != (IntPtr)0 && processInfo.hProcess != (IntPtr)INVALID_HANDLE_VALUE)
procSH.InitialSetHandle(processInfo.hProcess);
示例4: 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();
//.........这里部分代码省略.........