Ejemplo 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());
        }
Ejemplo 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());
        }