Beispiel #1
0
        private static ProcessApi.PROCESS_INFORMATION RunProcess(ref ProcessApi.STARTUPINFOEX sInfoEx, string commandLine)
        {
            var securityAttributeSize = Marshal.SizeOf <ProcessApi.SECURITY_ATTRIBUTES>();
            var pSec = new ProcessApi.SECURITY_ATTRIBUTES {
                nLength = securityAttributeSize
            };
            var tSec = new ProcessApi.SECURITY_ATTRIBUTES {
                nLength = securityAttributeSize
            };
            var success = CreateProcess(
                null,
                commandLine,
                ref pSec,
                ref tSec,
                false,
                EXTENDED_STARTUPINFO_PRESENT,
                IntPtr.Zero,
                null,
                ref sInfoEx,
                out var pInfo
                );

            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not create process.");
            }

            return(pInfo);
        }
Beispiel #2
0
        internal static SafeProcessHandle CreateProcessAsUserWithFlags(string lpApplicationName, string args, ProcessApi.CreateProcessFlags dwCreationFlags, out PROCESS_INFORMATION pInfo)
        {
            var sInfoEx = new ProcessApi.STARTUPINFOEX();

            sInfoEx.StartupInfo.cb = Marshal.SizeOf(sInfoEx);
            IntPtr lpValue = IntPtr.Zero;

            var pSec = new ProcessApi.SECURITY_ATTRIBUTES();
            var tSec = new ProcessApi.SECURITY_ATTRIBUTES();

            pSec.nLength = Marshal.SizeOf(pSec);
            tSec.nLength = Marshal.SizeOf(tSec);

            var command = $"{lpApplicationName} {args}";

            Logger.Instance.Log($"{nameof(CreateProcessAsUser)}: {lpApplicationName} {args}", LogLevel.Debug);
            if (!ProcessApi.CreateProcess(null, command, ref pSec, ref tSec, false, dwCreationFlags, IntPtr.Zero, null, ref sInfoEx, out pInfo))
            {
                throw new Win32Exception((int)ConsoleApi.GetLastError());
            }

            return(new SafeProcessHandle(pInfo.hProcess, true));
        }