Running a process under a different security context RRS feed

  • Question

  • Hello everyone,

    I need to launch a process with a different security context from an application running as a windows service.
    From what i have been reading, i will need to PInvoke the CreateProcessWithLogonW function.

    I have got some working code to do this:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Security.Permissions;
    using System.IO;

    namespace ExecTests2
        class Program
            enum LogonFlags
                LOGON_WITH_PROFILE = 0x00000001,
                LOGON_NETCREDENTIALS_ONLY = 0x00000002

            enum CreationFlags
                CREATE_SUSPENDED = 0x00000004,
                CREATE_NEW_CONSOLE = 0x00000010,
                CREATE_NEW_PROCESS_GROUP = 0x00000200,
                CREATE_UNICODE_ENVIRONMENT = 0x00000400,
                CREATE_SEPARATE_WOW_VDM = 0x00000800,
                CREATE_DEFAULT_ERROR_MODE = 0x04000000,

            struct ProcessInfo
                public IntPtr hProcess;
                public IntPtr hThread;
                public uint dwProcessId;
                public uint dwThreadId;

            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            struct StartupInfo
                public int cb;
                public string reserved1;
                public string desktop;
                public string title;
                public uint dwX;
                public uint dwY;
                public uint dwXSize;
                public uint dwYSize;
                public uint dwXCountChars;
                public uint dwYCountChars;
                public uint dwFillAttribute;
                public uint dwFlags;
                public ushort wShowWindow;
                public short reserved2;
                public int reserved3;
                public IntPtr hStdInput;
                public IntPtr hStdOutput;
                public IntPtr hStdError;

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
            static extern bool CreateProcessWithLogonW(
                string principal,
                string authority,
                string password,
                LogonFlags logonFlags,
                string appName,
                string cmdLine,
                CreationFlags creationFlags,
                IntPtr environmentBlock,
                string currentDirectory,
                ref StartupInfo startupInfo,
                out ProcessInfo processInfo);

            static extern bool CloseHandle(IntPtr h);

            static void Main(string[] args)
                StartupInfo si = new StartupInfo();
                si.cb = Marshal.SizeOf(typeof(StartupInfo));
                si.title = "This is impersonated command prompt";
                si.dwFlags = 0; // STARTF_USESHOWWINDOW;
                si.wShowWindow = 0; // SW_HIDE

                ProcessInfo pi = new ProcessInfo();
                string app = Path.Combine(Environment.SystemDirectory, "cmd.exe");

                if (CreateProcessWithLogonW("tmp", ".", "testing",
                    LogonFlags.LOGON_WITH_PROFILE, app, null, 0, IntPtr.Zero, null, ref si, out pi))
                else Console.WriteLine("Error code: {0}", Marshal.GetLastWin32Error());

    The problem is i need to:
    a) hide the window for the launched application
    b) redirect the stdout / stderr descriptors to a file

    Can anybody help me with this? I'm really desperate...

    Luís Silva
    Monday, June 2, 2008 3:47 PM