Ejemplo n.º 1
0
        bool StartWithCreateProcess(ProcessStartInfo startInfo)
        {
            if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardOutputEncodingNotAllowed));
            }

            if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardErrorEncodingNotAllowed));
            }

            if (this.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            var procInfo = new ProcInfo();

            if (startInfo.HaveEnvVars)
            {
                List <string> envVariables = new List <string> ();

                foreach (DictionaryEntry de in startInfo.EnvironmentVariables)
                {
                    if (de.Value == null)
                    {
                        continue;
                    }

                    envVariables.Add(string.Concat(
                                         (string)de.Key,
                                         "=",
                                         (string)de.Value));
                }

                procInfo.envVariables = envVariables.ToArray();
            }

            MonoIOError error;
            IntPtr      stdin_read = IntPtr.Zero, stdin_write = IntPtr.Zero;
            IntPtr      stdout_read = IntPtr.Zero, stdout_write = IntPtr.Zero;
            IntPtr      stderr_read = IntPtr.Zero, stderr_write = IntPtr.Zero;

            try {
                if (startInfo.RedirectStandardInput)
                {
                    CreatePipe(out stdin_read, out stdin_write, true);
                }
                else
                {
                    stdin_read  = MonoIO.ConsoleInput;
                    stdin_write = IntPtr.Zero;
                }

                if (startInfo.RedirectStandardOutput)
                {
                    CreatePipe(out stdout_read, out stdout_write, false);
                }
                else
                {
                    stdout_read  = IntPtr.Zero;
                    stdout_write = MonoIO.ConsoleOutput;
                }

                if (startInfo.RedirectStandardError)
                {
                    CreatePipe(out stderr_read, out stderr_write, false);
                }
                else
                {
                    stderr_read  = IntPtr.Zero;
                    stderr_write = MonoIO.ConsoleError;
                }

                FillUserInfo(startInfo, ref procInfo);

                //
                // FIXME: For redirected pipes we need to send descriptors of
                // stdin_write, stdout_read, stderr_read to child process and
                // close them there (fork makes exact copy of parent's descriptors)
                //
                if (!CreateProcess_internal(startInfo, stdin_read, stdout_write, stderr_write, ref procInfo))
                {
                    throw new Win32Exception(-procInfo.pid, "ApplicationName='" + startInfo.FileName + "', CommandLine='" + startInfo.Arguments +
                                             "', CurrentDirectory='" + startInfo.WorkingDirectory + "', Native error= " + Win32Exception.GetErrorMessage(-procInfo.pid));
                }
            } catch {
                if (startInfo.RedirectStandardInput)
                {
                    if (stdin_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stdin_read, out error);
                    }
                    if (stdin_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stdin_write, out error);
                    }
                }

                if (startInfo.RedirectStandardOutput)
                {
                    if (stdout_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stdout_read, out error);
                    }
                    if (stdout_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stdout_write, out error);
                    }
                }

                if (startInfo.RedirectStandardError)
                {
                    if (stderr_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stderr_read, out error);
                    }
                    if (stderr_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stderr_write, out error);
                    }
                }

                throw;
            } finally {
                if (procInfo.Password != IntPtr.Zero)
                {
                    Marshal.ZeroFreeBSTR(procInfo.Password);
                    procInfo.Password = IntPtr.Zero;
                }
            }

            SetProcessHandle(new SafeProcessHandle(procInfo.process_handle, true));
            SetProcessId(procInfo.pid);

#pragma warning disable 618

            if (startInfo.RedirectStandardInput)
            {
                MonoIO.Close(stdin_read, out error);

#if MOBILE
                var stdinEncoding = startInfo.StandardInputEncoding ?? Encoding.Default;
#else
                var stdinEncoding = startInfo.StandardInputEncoding ?? Console.InputEncoding;
#endif
                standardInput = new StreamWriter(new FileStream(stdin_write, FileAccess.Write, true, 8192), stdinEncoding)
                {
                    AutoFlush = true
                };
            }

            if (startInfo.RedirectStandardOutput)
            {
                MonoIO.Close(stdout_write, out error);

                Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.OutputEncoding;

                standardOutput = new StreamReader(new FileStream(stdout_read, FileAccess.Read, true, 8192), stdoutEncoding, true);
            }

            if (startInfo.RedirectStandardError)
            {
                MonoIO.Close(stderr_write, out error);

                Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.OutputEncoding;

                standardError = new StreamReader(new FileStream(stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true);
            }
#pragma warning restore

            return(true);
        }
 public override void WriteLine(string message)
 {
     this.TraceEvent(null, SR.GetString(SR.TraceAsTraceSource), TraceEventType.Information, 0, message);
 }
Ejemplo n.º 3
0
        bool StartWithShellExecuteEx(ProcessStartInfo startInfo)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            if (!String.IsNullOrEmpty(startInfo.UserName) || (startInfo.Password != null))
            {
                throw new InvalidOperationException(SR.GetString(SR.CantStartAsUser));
            }

            if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
            {
                throw new InvalidOperationException(SR.GetString(SR.CantRedirectStreams));
            }

            if (startInfo.StandardErrorEncoding != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardErrorEncodingNotAllowed));
            }

            if (startInfo.StandardOutputEncoding != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardOutputEncodingNotAllowed));
            }

            // can't set env vars with ShellExecuteEx...
            if (startInfo.environmentVariables != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.CantUseEnvVars));
            }

            ProcInfo proc_info = new ProcInfo();
            bool     ret;

            FillUserInfo(startInfo, ref proc_info);
            try {
                ret = ShellExecuteEx_internal(startInfo, ref proc_info);
            } finally {
                if (proc_info.Password != IntPtr.Zero)
                {
                    Marshal.ZeroFreeBSTR(proc_info.Password);
                }
                proc_info.Password = IntPtr.Zero;
            }
            if (!ret)
            {
                throw new Win32Exception(-proc_info.pid);
            }

            m_processHandle   = new SafeProcessHandle(proc_info.process_handle, true);
            haveProcessHandle = true;
            SetProcessId(proc_info.pid);

            if (watchForExit)
            {
                EnsureWatchingForExit();
            }

            return(ret);
        }
Ejemplo n.º 4
0
        bool StartWithCreateProcess(ProcessStartInfo startInfo)
        {
            if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardOutputEncodingNotAllowed));
            }

            if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardErrorEncodingNotAllowed));
            }

            if (this.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            var proc_info = new ProcInfo();

            if (startInfo.HaveEnvVars)
            {
                string [] strs = new string [startInfo.EnvironmentVariables.Count];
                startInfo.EnvironmentVariables.Keys.CopyTo(strs, 0);
                proc_info.envKeys = strs;

                strs = new string [startInfo.EnvironmentVariables.Count];
                startInfo.EnvironmentVariables.Values.CopyTo(strs, 0);
                proc_info.envValues = strs;
            }

            MonoIOError error;
            IntPtr      stdin_read = IntPtr.Zero, stdin_write = IntPtr.Zero;
            IntPtr      stdout_read = IntPtr.Zero, stdout_write = IntPtr.Zero;
            IntPtr      stderr_read = IntPtr.Zero, stderr_write = IntPtr.Zero;

            try {
                if (startInfo.RedirectStandardInput)
                {
                    CreatePipe(out stdin_read, out stdin_write, true);
                }
                else
                {
                    stdin_read  = MonoIO.ConsoleInput;
                    stdin_write = IntPtr.Zero;
                }

                if (startInfo.RedirectStandardOutput)
                {
                    CreatePipe(out stdout_read, out stdout_write, false);
                }
                else
                {
                    stdout_read  = IntPtr.Zero;
                    stdout_write = MonoIO.ConsoleOutput;
                }

                if (startInfo.RedirectStandardError)
                {
                    CreatePipe(out stderr_read, out stderr_write, false);
                }
                else
                {
                    stderr_read  = IntPtr.Zero;
                    stderr_write = MonoIO.ConsoleError;
                }

                FillUserInfo(startInfo, ref proc_info);

                //
                // FIXME: For redirected pipes we need to send descriptors of
                // stdin_write, stdout_read, stderr_read to child process and
                // close them there (fork makes exact copy of parent's descriptors)
                //
                if (!CreateProcess_internal(startInfo, stdin_read, stdout_write, stderr_write, ref proc_info))
                {
                    throw new Win32Exception(-proc_info.pid, "ApplicationName='" + startInfo.FileName + "', CommandLine='" + startInfo.Arguments +
                                             "', CurrentDirectory='" + startInfo.WorkingDirectory + "', Native error= " + Win32Exception.W32ErrorMessage(-proc_info.pid));
                }
            } catch {
                if (startInfo.RedirectStandardInput)
                {
                    if (stdin_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stdin_read, out error);
                    }
                    if (stdin_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stdin_write, out error);
                    }
                }

                if (startInfo.RedirectStandardOutput)
                {
                    if (stdout_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stdout_read, out error);
                    }
                    if (stdout_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stdout_write, out error);
                    }
                }

                if (startInfo.RedirectStandardError)
                {
                    if (stderr_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stderr_read, out error);
                    }
                    if (stderr_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stderr_write, out error);
                    }
                }

                throw;
            } finally {
                if (proc_info.Password != IntPtr.Zero)
                {
                    Marshal.ZeroFreeBSTR(proc_info.Password);
                    proc_info.Password = IntPtr.Zero;
                }
            }

            m_processHandle   = new SafeProcessHandle(proc_info.process_handle, true);
            haveProcessHandle = true;
            SetProcessId(proc_info.pid);

            if (startInfo.RedirectStandardInput)
            {
                //
                // FIXME: The descriptor needs to be closed but due to wapi io-layer
                // not coping with duplicated descriptors any StandardInput write fails
                //
                // MonoIO.Close (stdin_read, out error);

#if MOBILE
                var stdinEncoding = Encoding.Default;
#else
                var stdinEncoding = Console.InputEncoding;
#endif
                standardInput = new StreamWriter(new FileStream(stdin_write, FileAccess.Write, true, 8192), stdinEncoding)
                {
                    AutoFlush = true
                };
            }

            if (startInfo.RedirectStandardOutput)
            {
                MonoIO.Close(stdout_write, out error);

                Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.Out.Encoding;

                standardOutput = new StreamReader(new FileStream(stdout_read, FileAccess.Read, true, 8192), stdoutEncoding, true);
            }

            if (startInfo.RedirectStandardError)
            {
                MonoIO.Close(stderr_write, out error);

                Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.Out.Encoding;

                standardError = new StreamReader(new FileStream(stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true);
            }

            if (watchForExit)
            {
                EnsureWatchingForExit();
            }

            return(true);
        }
        internal unsafe CategorySample(byte[] data, System.Diagnostics.CategoryEntry entry, PerformanceCounterLib library)
        {
            this.entry   = entry;
            this.library = library;
            int nameIndex = entry.NameIndex;

            Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
            fixed(byte *numRef = data)
            {
                IntPtr ptr = new IntPtr((void *)numRef);

                Marshal.PtrToStructure(ptr, structure);
                this.SystemFrequency  = structure.PerfFreq;
                this.TimeStamp        = structure.PerfTime;
                this.TimeStamp100nSec = structure.PerfTime100nSec;
                ptr = (IntPtr)(((long)ptr) + structure.HeaderLength);
                int numObjectTypes = structure.NumObjectTypes;

                if (numObjectTypes == 0)
                {
                    this.CounterTable      = new Hashtable();
                    this.InstanceNameTable = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    return;
                }
                Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = null;
                bool flag = false;

                for (int i = 0; i < numObjectTypes; i++)
                {
                    perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
                    Marshal.PtrToStructure(ptr, perf_object_type);
                    if (perf_object_type.ObjectNameTitleIndex == nameIndex)
                    {
                        flag = true;
                        break;
                    }
                    ptr = (IntPtr)(((long)ptr) + perf_object_type.TotalByteLength);
                }
                if (!flag)
                {
                    throw new InvalidOperationException(SR.GetString("CantReadCategoryIndex", new object[] { nameIndex.ToString(CultureInfo.CurrentCulture) }));
                }
                this.CounterFrequency = perf_object_type.PerfFreq;
                this.CounterTimeStamp = perf_object_type.PerfTime;
                int numCounters  = perf_object_type.NumCounters;
                int numInstances = perf_object_type.NumInstances;

                if (numInstances == -1)
                {
                    this.IsMultiInstance = false;
                }
                else
                {
                    this.IsMultiInstance = true;
                }
                ptr = (IntPtr)(((long)ptr) + perf_object_type.HeaderLength);
                CounterDefinitionSample[] sampleArray = new CounterDefinitionSample[numCounters];
                this.CounterTable = new Hashtable(numCounters);
                for (int j = 0; j < sampleArray.Length; j++)
                {
                    Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
                    Marshal.PtrToStructure(ptr, perf_counter_definition);
                    sampleArray[j] = new CounterDefinitionSample(perf_counter_definition, this, numInstances);
                    ptr            = (IntPtr)(((long)ptr) + perf_counter_definition.ByteLength);
                    int counterType = sampleArray[j].CounterType;
                    if (!PerformanceCounterLib.IsBaseCounter(counterType))
                    {
                        if (counterType != 0x40000200)
                        {
                            this.CounterTable[sampleArray[j].NameIndex] = sampleArray[j];
                        }
                    }
                    else if (j > 0)
                    {
                        sampleArray[j - 1].BaseCounterDefinitionSample = sampleArray[j];
                    }
                }
                if (!this.IsMultiInstance)
                {
                    this.InstanceNameTable = new Hashtable(1, StringComparer.OrdinalIgnoreCase);
                    this.InstanceNameTable["systemdiagnosticsperfcounterlibsingleinstance"] = 0;
                    for (int k = 0; k < sampleArray.Length; k++)
                    {
                        sampleArray[k].SetInstanceValue(0, ptr);
                    }
                }
                else
                {
                    string[] instanceNamesFromIndex = null;
                    this.InstanceNameTable = new Hashtable(numInstances, StringComparer.OrdinalIgnoreCase);
                    for (int m = 0; m < numInstances; m++)
                    {
                        string str;
                        Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
                        Marshal.PtrToStructure(ptr, perf_instance_definition);
                        if ((perf_instance_definition.ParentObjectTitleIndex > 0) && (instanceNamesFromIndex == null))
                        {
                            instanceNamesFromIndex = this.GetInstanceNamesFromIndex(perf_instance_definition.ParentObjectTitleIndex);
                        }
                        if (((instanceNamesFromIndex != null) && (perf_instance_definition.ParentObjectInstance >= 0)) && (perf_instance_definition.ParentObjectInstance < (instanceNamesFromIndex.Length - 1)))
                        {
                            str = instanceNamesFromIndex[perf_instance_definition.ParentObjectInstance] + "/" + Marshal.PtrToStringUni((IntPtr)(((long)ptr) + perf_instance_definition.NameOffset));
                        }
                        else
                        {
                            str = Marshal.PtrToStringUni((IntPtr)(((long)ptr) + perf_instance_definition.NameOffset));
                        }
                        string key   = str;
                        int    num10 = 1;
                        while (true)
                        {
                            if (!this.InstanceNameTable.ContainsKey(key))
                            {
                                this.InstanceNameTable[key] = m;
                                break;
                            }
                            key = str + "#" + num10.ToString(CultureInfo.InvariantCulture);
                            num10++;
                        }
                        ptr = (IntPtr)(((long)ptr) + perf_instance_definition.ByteLength);
                        for (int n = 0; n < sampleArray.Length; n++)
                        {
                            sampleArray[n].SetInstanceValue(m, ptr);
                        }
                        ptr = (IntPtr)(((long)ptr) + Marshal.ReadInt32(ptr));
                    }
                }
            }
        }