for TARGET_J2EE only:
[mono.git] / mcs / class / System / System.Diagnostics / Process.cs
index 3953d51a6050763d6d14da4a736781d8d389db35..07ed17f498eeea26807abc464fa7e9ef98c0c6ad 100644 (file)
@@ -40,6 +40,7 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Security.Permissions;
 using System.Collections;
+using System.Security;
 using System.Threading;
 
 namespace System.Diagnostics {
@@ -48,6 +49,9 @@ namespace System.Diagnostics {
        [Designer ("System.Diagnostics.Design.ProcessDesigner, " + Consts.AssemblySystem_Design)]
        [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
        [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
+#if NET_2_0
+       [MonitoringDescription ("Represents a system process")]
+#endif
        public class Process : Component 
        {
                [StructLayout(LayoutKind.Sequential)]
@@ -64,6 +68,10 @@ namespace System.Diagnostics {
                        public int tid;
                        public string [] envKeys;
                        public string [] envValues;
+                       public string UserName;
+                       public string Domain;
+                       public IntPtr Password;
+                       public bool LoadUserProfile;
                };
                
                IntPtr process_handle;
@@ -96,6 +104,7 @@ namespace System.Diagnostics {
 
                void StartExitCallbackIfNeeded ()
                {
+#if !NET_2_1
                        bool start = (!already_waiting && enableRaisingEvents && exited_event != null);
                        if (start && process_handle != IntPtr.Zero && !HasExited) {
                                WaitOrTimerCallback cb = new WaitOrTimerCallback (CBOnExit);
@@ -103,6 +112,7 @@ namespace System.Diagnostics {
                                ThreadPool.RegisterWaitForSingleObject (h, cb, this, -1, true);
                                already_waiting = true;
                        }
+#endif
                }
 
                [DefaultValue (false), Browsable (false)]
@@ -199,9 +209,8 @@ namespace System.Diagnostics {
                [MonitoringDescription ("Process identifier.")]
                public int Id {
                        get {
-                               if (pid == 0) {
+                               if (pid == 0)
                                        throw new InvalidOperationException ("Process ID has not been set.");
-                               }
 
                                return(pid);
                        }
@@ -252,9 +261,10 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The maximum working set for this process.")]
                public IntPtr MaxWorkingSet {
                        get {
-                               if(HasExited) {
-                                       throw new InvalidOperationException("The process " + ProcessName + " (ID " + Id + ") has exited");
-                               }
+                               if(HasExited)
+                                       throw new InvalidOperationException(
+                                               "The process " + ProcessName +
+                                               " (ID " + Id + ") has exited");
                                
                                int min;
                                int max;
@@ -281,28 +291,27 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The minimum working set for this process.")]
                public IntPtr MinWorkingSet {
                        get {
-                               if(HasExited) {
-                                       throw new InvalidOperationException("The process " + ProcessName + " (ID " + Id + ") has exited");
-                               }
+                               if(HasExited)
+                                       throw new InvalidOperationException(
+                                               "The process " + ProcessName +
+                                               " (ID " + Id + ") has exited");
                                
                                int min;
                                int max;
-                               bool ok=GetWorkingSet_internal(process_handle, out min, out max);
-                               if(ok==false) {
+                               bool ok= GetWorkingSet_internal (process_handle, out min, out max);
+                               if(!ok)
                                        throw new Win32Exception();
-                               }
-                               
-                               return((IntPtr)min);
+                               return ((IntPtr) min);
                        }
                        set {
-                               if(HasExited) {
-                                       throw new InvalidOperationException("The process " + ProcessName + " (ID " + Id + ") has exited");
-                               }
+                               if(HasExited)
+                                       throw new InvalidOperationException(
+                                               "The process " + ProcessName +
+                                               " (ID " + Id + ") has exited");
                                
-                               bool ok=SetWorkingSet_internal(process_handle, value.ToInt32(), 0, true);
-                               if(ok==false) {
+                               bool ok = SetWorkingSet_internal (process_handle, value.ToInt32(), 0, true);
+                               if (!ok)
                                        throw new Win32Exception();
-                               }
                        }
                }
 
@@ -310,7 +319,7 @@ namespace System.Diagnostics {
                 * element 0.
                 */
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private extern ProcessModule[] GetModules_internal();
+               private extern ProcessModule[] GetModules_internal(IntPtr handle);
 
                private ProcessModuleCollection module_collection;
                
@@ -318,10 +327,9 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The modules that are loaded as part of this process.")]
                public ProcessModuleCollection Modules {
                        get {
-                               if(module_collection==null) {
-                                       module_collection=new ProcessModuleCollection(GetModules_internal());
-                               }
-
+                               if (module_collection == null)
+                                       module_collection = new ProcessModuleCollection(
+                                               GetModules_internal (process_handle));
                                return(module_collection);
                        }
                }
@@ -402,6 +410,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The number of bytes that are not pageable.")]
+               [ComVisible (false)]
                public long NonpagedSystemMemorySize64 {
                        get {
                                return(0);
@@ -411,6 +420,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The number of bytes that are paged.")]
+               [ComVisible (false)]
                public long PagedMemorySize64 {
                        get {
                                return(0);
@@ -420,6 +430,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The amount of paged system memory in bytes.")]
+               [ComVisible (false)]
                public long PagedSystemMemorySize64 {
                        get {
                                return(0);
@@ -429,6 +440,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The maximum amount of paged memory used by this process.")]
+               [ComVisible (false)]
                public long PeakPagedMemorySize64 {
                        get {
                                return(0);
@@ -438,6 +450,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The maximum amount of virtual memory used by this process.")]
+               [ComVisible (false)]
                public long PeakVirtualMemorySize64 {
                        get {
                                return(0);
@@ -447,6 +460,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The maximum amount of system memory used by this process.")]
+               [ComVisible (false)]
                public long PeakWorkingSet64 {
                        get {
                                return(0);
@@ -465,32 +479,71 @@ namespace System.Diagnostics {
                        }
                }
 
-               [MonoTODO]
+               [MonoLimitation ("Under Unix, only root is allowed to raise the priority.")]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The relative process priority.")]
                public ProcessPriorityClass PriorityClass {
                        get {
-                               return(ProcessPriorityClass.Normal);
+                               if (process_handle == IntPtr.Zero)
+                                       throw new InvalidOperationException ("Process has not been started.");
+                               
+                               int error;
+                               int prio = GetPriorityClass (process_handle, out error);
+                               if (prio == 0)
+                                       throw new Win32Exception (error);
+                               return (ProcessPriorityClass) prio;
                        }
                        set {
+                               if (!Enum.IsDefined (typeof (ProcessPriorityClass), value))
+                                       throw new InvalidEnumArgumentException (
+                                               "value", (int) value,
+                                               typeof (ProcessPriorityClass));
+
+                               if (process_handle == IntPtr.Zero)
+                                       throw new InvalidOperationException ("Process has not been started.");
+                               
+                               int error;
+                               if (!SetPriorityClass (process_handle, (int) value, out error))
+                                       throw new Win32Exception (error);
                        }
                }
 
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               static extern int GetPriorityClass (IntPtr handle, out int error);
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               static extern bool SetPriorityClass (IntPtr handle, int priority, out int error);
+
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The amount of memory exclusively used by this process.")]
+#if NET_2_0
+               [Obsolete ("Use PrivateMemorySize64")]
+#endif
                public int PrivateMemorySize {
                        get {
                                return(0);
                        }
                }
 
-               [MonoTODO]
+#if NET_2_0
+               [MonoNotSupported ("")]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               [MonitoringDescription ("The session ID for this process.")]
+               public int SessionId {
+                       get { throw new NotImplementedException (); }
+               }
+#endif
+
+               /* the meaning of type is as follows: 0: user, 1: system, 2: total */
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               private extern static long Times (IntPtr handle, int type);
+
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The amount of processing time spent in the OS core for this process.")]
                public TimeSpan PrivilegedProcessorTime {
                        get {
-                               return(new TimeSpan(0));
+                               return new TimeSpan (Times (process_handle, 1));
                        }
                }
 
@@ -509,9 +562,8 @@ namespace System.Diagnostics {
                                         * null, assume the process
                                         * has exited
                                         */
-                                       if(process_name==null) {
+                                       if (process_name == null)
                                                throw new SystemException("The process has exited");
-                                       }
                                        
                                        /* Strip the suffix (if it
                                         * exists) simplistically
@@ -556,9 +608,9 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The standard error stream of this process.")]
                public StreamReader StandardError {
                        get {
-                               if (error_stream == null) {
+                               if (error_stream == null)
                                        throw new InvalidOperationException("Standard error has not been redirected");
-                               }
+
 #if NET_2_0
                                if ((async_mode & AsyncModes.AsyncError) != 0)
                                        throw new InvalidOperationException ("Cannot mix asynchronous and synchonous reads.");
@@ -576,9 +628,8 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The standard input stream of this process.")]
                public StreamWriter StandardInput {
                        get {
-                               if (input_stream == null) {
+                               if (input_stream == null)
                                        throw new InvalidOperationException("Standard input has not been redirected");
-                               }
 
                                return(input_stream);
                        }
@@ -590,9 +641,9 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The standard output stream of this process.")]
                public StreamReader StandardOutput {
                        get {
-                               if (output_stream == null) {
+                               if (output_stream == null)
                                        throw new InvalidOperationException("Standard output has not been redirected");
-                               }
+
 #if NET_2_0
                                if ((async_mode & AsyncModes.AsyncOutput) != 0)
                                        throw new InvalidOperationException ("Cannot mix asynchronous and synchonous reads.");
@@ -610,18 +661,14 @@ namespace System.Diagnostics {
                [MonitoringDescription ("Information for the start of this process.")]
                public ProcessStartInfo StartInfo {
                        get {
-                               if(start_info==null) {
-                                       start_info=new ProcessStartInfo();
-                               }
-                               
-                               return(start_info);
+                               if (start_info == null)
+                                       start_info = new ProcessStartInfo();
+                               return start_info;
                        }
                        set {
-                               if(value==null) {
-                                       throw new ArgumentException("value is null");
-                               }
-                               
-                               start_info=value;
+                               if (value == null)
+                                       throw new ArgumentNullException("value");
+                               start_info = value;
                        }
                }
 
@@ -651,25 +698,23 @@ namespace System.Diagnostics {
                [MonitoringDescription ("The number of threads of this process.")]
                public ProcessThreadCollection Threads {
                        get {
-                               return(null);
+                               return ProcessThreadCollection.GetEmpty ();
                        }
                }
 
-               [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The total CPU time spent for this process.")]
                public TimeSpan TotalProcessorTime {
                        get {
-                               return(new TimeSpan(0));
+                               return new TimeSpan (Times (process_handle, 2));
                        }
                }
 
-               [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The CPU time spent for this process in user mode.")]
                public TimeSpan UserProcessorTime {
                        get {
-                               return(new TimeSpan(0));
+                               return new TimeSpan (Times (process_handle, 0));
                        }
                }
 
@@ -698,9 +743,20 @@ namespace System.Diagnostics {
                }
 
 #if NET_2_0
+               [MonoTODO]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               [MonitoringDescription ("The amount of memory exclusively used by this process.")]
+               [ComVisible (false)]
+               public long PrivateMemorySize64 {
+                       get {
+                               return(0);
+                       }
+               }
+
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The amount of virtual memory currently used for this process.")]
+               [ComVisible (false)]
                public long VirtualMemorySize64 {
                        get {
                                return(0);
@@ -710,6 +766,7 @@ namespace System.Diagnostics {
                [MonoTODO]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The amount of physical memory currently used for this process.")]
+               [ComVisible (false)]
                public long WorkingSet64 {
                        get {
                                return(0);
@@ -753,42 +810,49 @@ namespace System.Diagnostics {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static int GetPid_internal();
 
-               public static Process GetCurrentProcess() {
-                       int pid=GetPid_internal();
-                       IntPtr proc=GetProcess_internal(pid);
+               public static Process GetCurrentProcess()
+               {
+                       int pid = GetPid_internal();
+                       IntPtr proc = GetProcess_internal(pid);
                        
-                       if(proc==IntPtr.Zero) {
+                       if (proc == IntPtr.Zero)
                                throw new SystemException("Can't find current process");
-                       }
 
-                       return(new Process(proc, pid));
+                       return (new Process (proc, pid));
                }
 
-               public static Process GetProcessById(int processId) {
-                       IntPtr proc=GetProcess_internal(processId);
+               public static Process GetProcessById(int processId)
+               {
+                       IntPtr proc = GetProcess_internal(processId);
                        
-                       if(proc==IntPtr.Zero) {
-                               throw new ArgumentException("Can't find process with ID " + processId.ToString());
-                       }
+                       if (proc == IntPtr.Zero)
+                               throw new ArgumentException ("Can't find process with ID " + processId.ToString ());
 
-                       return(new Process(proc, processId));
+                       return (new Process (proc, processId));
                }
 
-               [MonoTODO]
+               [MonoTODO ("There is no support for retrieving process information from a remote machine")]
                public static Process GetProcessById(int processId, string machineName) {
-                       throw new NotImplementedException();
+                       if (machineName == null)
+                               throw new ArgumentNullException ("machineName");
+
+                       if (!IsLocalMachine (machineName))
+                               throw new NotImplementedException ();
+
+                       return GetProcessById (processId);
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static int[] GetProcesses_internal();
 
-               public static Process[] GetProcesses() {
-                       int[] pids=GetProcesses_internal();
-                       ArrayList proclist=new ArrayList();
+               public static Process[] GetProcesses()
+               {
+                       int [] pids = GetProcesses_internal ();
+                       ArrayList proclist = new ArrayList ();
                        
-                       for(int i=0; i<pids.Length; i++) {
+                       for (int i = 0; i < pids.Length; i++) {
                                try {
-                                       proclist.Add(GetProcessById(pids[i]));
+                                       proclist.Add (GetProcessById (pids [i]));
                                } catch (SystemException) {
                                        /* The process might exit
                                         * between
@@ -798,28 +862,35 @@ namespace System.Diagnostics {
                                }
                        }
 
-                       return((Process[])proclist.ToArray(typeof(Process)));
+                       return ((Process []) proclist.ToArray (typeof (Process)));
                }
 
-               [MonoTODO]
+               [MonoTODO ("There is no support for retrieving process information from a remote machine")]
                public static Process[] GetProcesses(string machineName) {
-                       throw new NotImplementedException();
+                       if (machineName == null)
+                               throw new ArgumentNullException ("machineName");
+
+                       if (!IsLocalMachine (machineName))
+                               throw new NotImplementedException ();
+
+                       return GetProcesses ();
                }
 
-               public static Process[] GetProcessesByName(string processName) {
-                       Process[] procs=GetProcesses();
-                       ArrayList proclist=new ArrayList();
+               public static Process[] GetProcessesByName(string processName)
+               {
+                       Process [] procs = GetProcesses();
+                       ArrayList proclist = new ArrayList();
                        
-                       for(int i=0; i<procs.Length; i++) {
+                       for (int i = 0; i < procs.Length; i++) {
                                /* Ignore case */
-                               if(String.Compare(processName,
-                                                 procs[i].ProcessName,
-                                                 true)==0) {
-                                       proclist.Add(procs[i]);
+                               if (String.Compare (processName,
+                                                   procs [i].ProcessName,
+                                                   true) == 0) {
+                                       proclist.Add (procs [i]);
                                }
                        }
 
-                       return((Process[])proclist.ToArray(typeof(Process)));
+                       return ((Process[]) proclist.ToArray (typeof(Process)));
                }
 
                [MonoTODO]
@@ -836,8 +907,10 @@ namespace System.Diagnostics {
                public static void LeaveDebugMode() {
                }
 
-               [MonoTODO]
-               public void Refresh() {
+               public void Refresh ()
+               {
+                       // FIXME: should refresh any cached data we might have about
+                       // the process (currently we have none).
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -863,12 +936,18 @@ namespace System.Diagnostics {
                                throw new InvalidOperationException ("UseShellExecute must be false when redirecting I/O.");
                        }
 
-                       if (startInfo.HaveEnvVars) {
+                       if (startInfo.HaveEnvVars)
                                throw new InvalidOperationException ("UseShellExecute must be false in order to use environment variables.");
-                       }
 
-                       ret = ShellExecuteEx_internal (startInfo,
-                                                      ref proc_info);
+                       FillUserInfo (startInfo, ref proc_info);
+                       try {
+                               ret = ShellExecuteEx_internal (startInfo,
+                                                              ref proc_info);
+                       } finally {
+                               if (proc_info.Password != IntPtr.Zero)
+                                       Marshal.FreeBSTR (proc_info.Password);
+                               proc_info.Password = IntPtr.Zero;
+                       }
                        if (!ret) {
                                throw new Win32Exception (-proc_info.pid);
                        }
@@ -884,8 +963,6 @@ namespace System.Diagnostics {
                private static bool Start_noshell (ProcessStartInfo startInfo,
                                                   Process process)
                {
-                       if (Path.IsPathRooted (startInfo.FileName) && !File.Exists (startInfo.FileName))
-                               throw new FileNotFoundException  ("Executable not found: " + startInfo.FileName);
                        ProcInfo proc_info=new ProcInfo();
                        IntPtr stdin_rd, stdin_wr;
                        IntPtr stdout_wr;
@@ -959,10 +1036,17 @@ namespace System.Diagnostics {
                                process.stderr_rd = (IntPtr)0;
                                stderr_wr = MonoIO.ConsoleError;
                        }
-                       
-                       ret = CreateProcess_internal (startInfo,
-                                                     stdin_rd, stdout_wr, stderr_wr,
-                                                     ref proc_info);
+
+                       FillUserInfo (startInfo, ref proc_info);
+                       try {
+                               ret = CreateProcess_internal (startInfo,
+                                                             stdin_rd, stdout_wr, stderr_wr,
+                                                             ref proc_info);
+                       } finally {
+                               if (proc_info.Password != IntPtr.Zero)
+                                       Marshal.FreeBSTR (proc_info.Password);
+                               proc_info.Password = IntPtr.Zero;
+                       }
                        if (!ret) {
                                if (startInfo.RedirectStandardInput == true) {
                                        MonoIO.Close (stdin_rd, out error);
@@ -979,11 +1063,10 @@ namespace System.Diagnostics {
                                        MonoIO.Close (stderr_wr, out error);
                                }
 
-                               throw new Win32Exception (-proc_info.pid, 
-                                       "ApplicationName='"+startInfo.FileName+
-                                       "', CommandLine='"+startInfo.Arguments+
-                                       "', CurrentDirectory='"+startInfo.WorkingDirectory+
-                                       "', PATH='"+startInfo.EnvironmentVariables["PATH"]+"'");
+                               throw new Win32Exception (-proc_info.pid,
+                                       "ApplicationName='" + startInfo.FileName +
+                                       "', CommandLine='" + startInfo.Arguments +
+                                       "', CurrentDirectory='" + startInfo.WorkingDirectory + "'");
                        }
 
                        process.process_handle = proc_info.process_handle;
@@ -991,18 +1074,26 @@ namespace System.Diagnostics {
                        
                        if (startInfo.RedirectStandardInput == true) {
                                MonoIO.Close (stdin_rd, out error);
-                               process.input_stream = new StreamWriter (new FileStream (stdin_wr, FileAccess.Write, true));
+                               process.input_stream = new StreamWriter (new MonoSyncFileStream (stdin_wr, FileAccess.Write, true, 8192), Console.Out.Encoding);
                                process.input_stream.AutoFlush = true;
                        }
 
+#if NET_2_0
+                       Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.Out.Encoding;
+                       Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.Out.Encoding;
+#else
+                       Encoding stdoutEncoding = Console.Out.Encoding;
+                       Encoding stderrEncoding = stdoutEncoding;
+#endif
+
                        if (startInfo.RedirectStandardOutput == true) {
                                MonoIO.Close (stdout_wr, out error);
-                               process.output_stream = new StreamReader (new FileStream (process.stdout_rd, FileAccess.Read, true));
+                               process.output_stream = new StreamReader (new MonoSyncFileStream (process.stdout_rd, FileAccess.Read, true, 8192), stdoutEncoding);
                        }
 
                        if (startInfo.RedirectStandardError == true) {
                                MonoIO.Close (stderr_wr, out error);
-                               process.error_stream = new StreamReader (new FileStream (process.stderr_rd, FileAccess.Read, true));
+                               process.error_stream = new StreamReader (new MonoSyncFileStream (process.stderr_rd, FileAccess.Read, true, 8192), stderrEncoding);
                        }
 
                        process.StartExitCallbackIfNeeded ();
@@ -1010,54 +1101,95 @@ namespace System.Diagnostics {
                        return(ret);
                }
 
+               // Note that ProcInfo.Password must be freed.
+               private static void FillUserInfo (ProcessStartInfo startInfo, ref ProcInfo proc_info)
+               {
+#if NET_2_0
+                       if (startInfo.UserName != null) {
+                               proc_info.UserName = startInfo.UserName;
+                               proc_info.Domain = startInfo.Domain;
+                               if (startInfo.Password != null)
+                                       proc_info.Password = Marshal.SecureStringToBSTR (startInfo.Password);
+                               else
+                                       proc_info.Password = IntPtr.Zero;
+                               proc_info.LoadUserProfile = startInfo.LoadUserProfile;
+                       }
+#endif
+               }
+
                private static bool Start_common (ProcessStartInfo startInfo,
                                                  Process process)
                {
-                       if(startInfo.FileName == null ||
-                          startInfo.FileName == "") {
+                       if (startInfo.FileName == null || startInfo.FileName.Length == 0)
                                throw new InvalidOperationException("File name has not been set");
-                       }
+                       
+#if NET_2_0
+                       if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
+                               throw new InvalidOperationException ("StandardErrorEncoding is only supported when standard error is redirected");
+                       if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
+                               throw new InvalidOperationException ("StandardOutputEncoding is only supported when standard output is redirected");
+#endif
                        
                        if (startInfo.UseShellExecute) {
+#if NET_2_0
+                               if (startInfo.UserName != null)
+                                       throw new InvalidOperationException ("UserShellExecute must be false if an explicit UserName is specified when starting a process");
+#endif
                                return (Start_shell (startInfo, process));
                        } else {
                                return (Start_noshell (startInfo, process));
                        }
                }
                
-               public bool Start() {
-                       bool ret;
-                       
-                       ret=Start_common(start_info, this);
-                       
-                       return(ret);
+               public bool Start ()
+               {
+                       if (process_handle != IntPtr.Zero) {
+                               Process_free_internal (process_handle);
+                               process_handle = IntPtr.Zero;
+                       }
+                       return Start_common(start_info, this);
                }
 
-               public static Process Start(ProcessStartInfo startInfo) {
+               public static Process Start (ProcessStartInfo startInfo)
+               {
+                       if (startInfo == null)
+                               throw new ArgumentNullException ("startInfo");
+
                        Process process=new Process();
-                       bool ret;
+                       process.StartInfo = startInfo;
+                       if (Start_common(startInfo, process))
+                               return process;
+                       return null;
+               }
 
-                       ret=Start_common(startInfo, process);
-                       
-                       if(ret==true) {
-                               return(process);
-                       } else {
-                               return(null);
-                       }
+               public static Process Start (string fileName)
+               {
+                       return Start (new ProcessStartInfo (fileName));
                }
 
-               public static Process Start(string fileName) {
-                       return Start(new ProcessStartInfo(fileName));
+               public static Process Start(string fileName, string arguments)
+               {
+                       return Start (new ProcessStartInfo (fileName, arguments));
                }
 
-               public static Process Start(string fileName,
-                                           string arguments) {
-                       return Start(new ProcessStartInfo(fileName, arguments));
+#if NET_2_0
+               public static Process Start(string fileName, string username, SecureString password, string domain) {
+                       return Start(fileName, null, username, password, domain);
                }
 
-               public override string ToString() {
-                       return(base.ToString() +
-                              " (" + this.ProcessName + ")");
+               public static Process Start(string fileName, string arguments, string username, SecureString password, string domain) {
+                       ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments);
+                       psi.UserName = username;
+                       psi.Password = password;
+                       psi.Domain = domain;
+                       psi.UseShellExecute = false;
+                       return Start(psi);
+               }
+#endif
+
+               public override string ToString()
+               {
+                       return(base.ToString() + " (" + this.ProcessName + ")");
                }
 
                /* Waits up to ms milliseconds for process 'handle' to
@@ -1115,9 +1247,20 @@ namespace System.Diagnostics {
                        return(false);
                }
 
+               private static bool IsLocalMachine (string machineName)
+               {
+                       if (machineName == "." || machineName.Length == 0)
+                               return true;
+
+                       return (string.Compare (machineName, Environment.MachineName, true) == 0);
+               }
 
 #if NET_2_0
+               [Browsable (true)]
+               [MonitoringDescription ("Raised when it receives output data")]
                public event DataReceivedEventHandler OutputDataReceived;
+               [Browsable (true)]
+               [MonitoringDescription ("Raised when it receives error data")]
                public event DataReceivedEventHandler ErrorDataReceived;
 
                void OnOutputDataReceived (string str)
@@ -1237,11 +1380,12 @@ namespace System.Diagnostics {
                                }
 
                                string end = strs [len - 1];
-                               if (last || end == "") {
-                                       if (err_out)
+                               if (last || (len == 1 && end == "")) {
+                                       if (err_out) {
                                                process.OnOutputDataReceived (end);
-                                       else
+                                       } else {
                                                process.OnErrorDataReceived (end);
+                                       }
                                } else {
                                        sb.Append (end);
                                }