Exemplo n.º 1
0
 public static extern int CreateProcessAsUser(SafeHandle hToken, string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, HandleRef lpEnvironment, string lpCurrentDirectory, NativeMethods.STARTUPINFO lpStartupInfo, NativeMethods.PROCESS_INFORMATION lpProcessInformation);
Exemplo n.º 2
0
        public IProcess CreateProcess(ProcessStartInfo startInfo)
        {
            Invariant.ArgumentNotNull((object)startInfo, "startInfo");
            if (startInfo.UseShellExecute || string.IsNullOrEmpty(startInfo.UserName))
            {
                return((IProcess) new ProcessWrapper(Process.Start(startInfo)));
            }
            if (string.IsNullOrEmpty(startInfo.FileName))
            {
                throw new InvalidOperationException("A file name must be provided for creating a process.");
            }
            IntPtr zero = IntPtr.Zero;
            IntPtr num  = startInfo.Password != null?Marshal.SecureStringToCoTaskMemUnicode(startInfo.Password) : Marshal.StringToCoTaskMemUni(string.Empty);

            try
            {
                SafeFileHandle phToken;
                if (NativeMethods.LogonUser(startInfo.UserName, startInfo.Domain, num, 2, 0, out phToken) == 0)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                int dwCreationFlags = 0;
                if (startInfo.CreateNoWindow)
                {
                    dwCreationFlags |= 134217728;
                }
                IntPtr   handle1  = IntPtr.Zero;
                GCHandle gcHandle = new GCHandle();
                if (startInfo.EnvironmentVariables != null)
                {
                    bool unicode = false;
                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    {
                        dwCreationFlags |= 1024;
                        unicode          = true;
                    }
                    gcHandle = GCHandle.Alloc((object)this.CreateEnvironmentBlock(startInfo.EnvironmentVariables, unicode), GCHandleType.Pinned);
                    handle1  = gcHandle.AddrOfPinnedObject();
                }
                string lpCommandLine      = this.BuildCommandLine(startInfo.FileName, startInfo.Arguments);
                string lpCurrentDirectory = string.IsNullOrEmpty(startInfo.WorkingDirectory) ? Environment.CurrentDirectory : startInfo.WorkingDirectory;
                NativeMethods.STARTUPINFO         lpStartupInfo        = new NativeMethods.STARTUPINFO();
                NativeMethods.PROCESS_INFORMATION lpProcessInformation = new NativeMethods.PROCESS_INFORMATION();
                try
                {
                    if (NativeMethods.CreateProcessAsUser((SafeHandle)phToken, (string)null, lpCommandLine, IntPtr.Zero, IntPtr.Zero, false, dwCreationFlags, new HandleRef((object)null, handle1), lpCurrentDirectory, lpStartupInfo, lpProcessInformation) == 0)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                finally
                {
                    phToken.Close();
                    lpStartupInfo.Dispose();
                    if (gcHandle.IsAllocated)
                    {
                        gcHandle.Free();
                    }
                }
                Process processById = Process.GetProcessById(lpProcessInformation.dwProcessId);
                IntPtr  handle2     = processById.Handle;
                NativeMethods.CloseHandle(lpProcessInformation.hProcess);
                NativeMethods.CloseHandle(lpProcessInformation.hThread);
                return((IProcess) new ProcessWrapper(processById));
            }
            finally
            {
                if (num != IntPtr.Zero)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode(num);
                }
            }
        }