示例#1
0
        public static uint CreateProcess(string userName, string domain, string password, string application, string workingDirectory, bool showMinimized = false)
        {
            TraceFactory.Logger.Debug("Creating user process for {0}/{1} - {2}".FormatWith(domain, userName, application));
            try
            {
                // Default flags for the impersonation startup.
                NativeMethods.LogonFlags    logonFlags    = NativeMethods.LogonFlags.LogonWithProfile;
                NativeMethods.CreationFlags creationFlags = 0;
                IntPtr environment      = IntPtr.Zero;
                string currentDirectory = workingDirectory;
                string commandLine      = application;

                NativeMethods.StartupInfo startupInfo = new NativeMethods.StartupInfo();
                startupInfo.Cb = Marshal.SizeOf(typeof(NativeMethods.StartupInfo));

                if (showMinimized)
                {
                    startupInfo.Flags      = (uint)NativeMethods.StartupInfoFlags.StartfUseShowWindow;
                    startupInfo.ShowWindow = (ushort)NativeMethods.StartupInfoFlags.SWMinimize;
                }

                NativeMethods.ProcessInfo processInfo = new NativeMethods.ProcessInfo();

                TraceFactory.Logger.Debug("Calling CreateProcessWithLogonW...");
                bool created = NativeMethods.CreateProcessWithLogonW(userName, domain, password, logonFlags, null,
                                                                     commandLine, creationFlags, environment, currentDirectory, ref startupInfo, out processInfo);

                if (created)
                {
                    TraceFactory.Logger.Debug("Process ({0}) successfully created, closing handles".FormatWith(processInfo.ProcessId));
                    NativeMethods.CloseHandle(processInfo.ProcessPtr);
                    NativeMethods.CloseHandle(processInfo.ThreadPtr);
                }
                else
                {
                    int error = Marshal.GetLastWin32Error();
                    var msg   = "Unable to create Process, error code {3}: {0}, {1}, {2}".FormatWith(userName, domain, password, error);
                    TraceFactory.Logger.Debug(msg);
                    throw new Exception(msg);
                }

                return(processInfo.ProcessId);
            }
            catch (Exception ex)
            {
                // TODO: Need to do something here...
                TraceFactory.Logger.Fatal("Failed to create process", ex);
                throw;
            }
        }
示例#2
0
        private static void StartProcessWithLogonNetOnly(ProcessStartInfo startInfo, bool waitForExit)
        {
            if (startInfo.UseShellExecute)
            {
                throw new InvalidOperationException("UseShellExecute must be false.");
            }

            if (startInfo.LoadUserProfile)
            {
                throw new InvalidOperationException("LoadUserProfile cannot be used.");
            }

            if (string.IsNullOrEmpty(startInfo.UserName))
            {
                throw new InvalidOperationException("UserName is empty.");
            }

            var cmdLine              = BuildCommandLine(startInfo.FileName, startInfo.Arguments);
            var lpStartupInfo        = new NativeMethods.STARTUPINFO();
            var lpProcessInformation = new NativeMethods.PROCESS_INFORMATION();

            int creationFlags = 0;

            if (startInfo.CreateNoWindow)
            {
                creationFlags |= 0x8000000;
            }

            IntPtr zero = IntPtr.Zero;

            string workingDirectory = startInfo.WorkingDirectory;

            if (string.IsNullOrEmpty(workingDirectory))
            {
                workingDirectory = Environment.CurrentDirectory;
            }

            NativeMethods.LogonFlags logonFlags = NativeMethods.LogonFlags.LOGON_NETCREDENTIALS_ONLY;   //NetOnly;

            IntPtr passwordPrt = IntPtr.Zero;

            try
            {
                if (startInfo.Password == null)
                {
                    passwordPrt = Marshal.StringToCoTaskMemUni(string.Empty);
                }
                else
                {
                    passwordPrt = Marshal.SecureStringToCoTaskMemUnicode(startInfo.Password);
                }

                int  error = 0;
                bool flag  = NativeMethods.CreateProcessWithLogonW(startInfo.UserName, startInfo.Domain, passwordPrt, logonFlags, null, cmdLine, creationFlags, zero, workingDirectory, lpStartupInfo, lpProcessInformation);
                if (!flag)
                {
                    error = Marshal.GetLastWin32Error();
                }

                if (!flag)
                {
                    if (error != 0xc1 && error != 0xd8)
                    {
                        throw new Win32Exception(error);
                    }
                    throw new Win32Exception(error, "Invalid Application");
                }
            }
            finally
            {
                if (passwordPrt != IntPtr.Zero)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode(passwordPrt);
                }
            }

            if (waitForExit)
            {
                NativeMethods.WaitForSingleObject(lpProcessInformation.hProcess, 0xFFFFFFFF);
            }
        }