Esempio n. 1
0
        public static WindowsApi.PROCESS_INFORMATION CreateProcess(ProcessStartInfo startInfo, string desktopName = null, int?millisecondsToWait = 100)
        {
            var startupInfo = new WindowsApi.STARTUPINFO();

            startupInfo.cb        = Marshal.SizeOf(startupInfo);
            startupInfo.lpDesktop = desktopName;

            var processInformation = new WindowsApi.PROCESS_INFORMATION();

            bool result = WindowsApi.CreateProcess(startInfo.FileName,
                                                   startInfo.Arguments,
                                                   IntPtr.Zero,
                                                   IntPtr.Zero,
                                                   true,
                                                   WindowsApi.NORMAL_PRIORITY_CLASS,
                                                   IntPtr.Zero,
                                                   startInfo.WorkingDirectory,
                                                   ref startupInfo,
                                                   ref processInformation);

            if (result)
            {
                if (millisecondsToWait.HasValue)
                {
                    WindowsApi.WaitForInputIdle(processInformation.hProcess, (uint)millisecondsToWait.Value);
                }

                WindowsApi.CloseHandle(processInformation.hThread);
                return(processInformation);
            }

            return(new WindowsApi.PROCESS_INFORMATION());
        }
Esempio n. 2
0
        public static WindowsApi.PROCESS_INFORMATION CreateProcess(ITorSharpProxy proxy, ProcessStartInfo startInfo, string desktopName = null, int?millisecondsToWait = 100)
        {
            // Source: https://stackoverflow.com/a/59387440/3286975
            var startupInfo = new WindowsApi.STARTUPINFO();

            startupInfo.cb        = Marshal.SizeOf(startupInfo);
            startupInfo.lpDesktop = desktopName;

            var processInformation = new WindowsApi.PROCESS_INFORMATION();

            string command = startInfo.FileName + " " + startInfo.Arguments;

            WindowsApi.SECURITY_ATTRIBUTES saAttr = new WindowsApi.SECURITY_ATTRIBUTES
            {
                nLength              = (uint)Marshal.SizeOf(typeof(WindowsApi.SECURITY_ATTRIBUTES)),
                bInheritHandle       = 0x1,
                lpSecurityDescriptor = IntPtr.Zero
            };

            WindowsApi.CreatePipe(ref out_read, ref out_write, ref saAttr, 0);
            WindowsApi.CreatePipe(ref err_read, ref err_write, ref saAttr, 0);

            WindowsApi.SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0);
            WindowsApi.SetHandleInformation(err_read, HANDLE_FLAG_INHERIT, 0);

            bool result = WindowsApi.CreateProcess(null,
                                                   command,
                                                   IntPtr.Zero,
                                                   IntPtr.Zero,
                                                   true,
                                                   WindowsApi.NORMAL_PRIORITY_CLASS,
                                                   IntPtr.Zero,
                                                   startInfo.WorkingDirectory,
                                                   ref startupInfo,
                                                   ref processInformation);

            var thread = new Thread(() => RedirectStd(proxy));

            thread.Start();

            if (result)
            {
                if (millisecondsToWait.HasValue)
                {
                    WindowsApi.WaitForInputIdle(processInformation.hProcess, (uint)millisecondsToWait.Value);
                }

                WindowsApi.CloseHandle(processInformation.hThread);
                return(processInformation);
            }

            return(new WindowsApi.PROCESS_INFORMATION());
        }
Esempio n. 3
0
        private static void RedirectStd(ITorSharpProxy proxy)
        {
            // Source: https://stackoverflow.com/a/59387440/3286975

            byte[] out_buf = new byte[BUFSIZE];
            byte[] err_buf = new byte[BUFSIZE];

            int dwRead = 0;

            string out_str = "";
            string err_str = "";

            bool isOutputSet = proxy.GetHandler(true) != null;
            bool isErrorSet  = proxy.GetHandler(false) != null;

            while (true)
            {
                bool bSuccess;

                if (isOutputSet)
                {
                    bSuccess = WindowsApi.ReadFile(out_read, out_buf, BUFSIZE, ref dwRead, IntPtr.Zero);
                    if (!bSuccess || dwRead == 0)
                    {
                        break;
                    }

                    out_str += System.Text.Encoding.Default.GetString(out_buf);
                    out_str  = PushCallback(proxy, out_str, true);
                }

                if (isErrorSet)
                {
                    bSuccess = WindowsApi.ReadFile(out_read, err_buf, BUFSIZE, ref dwRead, IntPtr.Zero);
                    if (!bSuccess || dwRead == 0)
                    {
                        break;
                    }

                    err_str += System.Text.Encoding.Default.GetString(err_buf);
                    err_str  = PushCallback(proxy, err_str, true);
                }
            }

            WindowsApi.CloseHandle(out_read);
            WindowsApi.CloseHandle(err_read);
            WindowsApi.CloseHandle(out_write);
            WindowsApi.CloseHandle(err_write);
        }