public static Process Start(int sessionId, string applicationName, string commandLine)
        {
            var hToken = IntPtr.Zero;
            if (WTSQueryUserToken(sessionId, ref hToken) == 0)
            {
                throw new Win32Exception();
            }

            var hAdminToken = hToken;
            if (UserAccountControlExists)
            {
                var linked = new TOKEN_LINKED_TOKEN();
                int returned;
                if (
                    GetTokenInformation(hToken, TokenInformationClass.TokenLinkedToken, ref linked,
                                        Marshal.SizeOf(linked), out returned) == 0)
                {
                    throw new Win32Exception();
                }

                var hLinkedToken = linked.LinkedToken;
                if (
                    DuplicateTokenEx(hLinkedToken, 0, IntPtr.Zero, SecurityImpersonationLevel.SecurityImpersonation,
                                     TokenType.TokenPrimary, out hAdminToken) == 0)
                {
                    throw new Win32Exception();
                }

                CloseHandle(hLinkedToken);
                CloseHandle(hToken);
            }

            try
            {
                var commandLineText = new StringBuilder(string.Format("\"{0}\" {1}", applicationName, commandLine));
                var info = new StartupInfo();
                info.Cb = Marshal.SizeOf(info);
                ProcessInformation processInfo;

                IntPtr hEnvironment;
                if (CreateEnvironmentBlock(out hEnvironment, hAdminToken, true) == 0)
                {
                    throw new Win32Exception();
                }

                try
                {
                    if (
                        CreateProcessAsUser(hAdminToken, null, commandLineText, IntPtr.Zero, IntPtr.Zero, false,
                                            CreationFlags.UnicodeEnvironment, hEnvironment, null, ref info,
                                            out processInfo) == 0)
                    {
                        throw new Win32Exception();
                    }
                }
                finally
                {
                    DestroyEnvironmentBlock(hEnvironment);
                }

                CloseHandle(processInfo.Process);
                CloseHandle(processInfo.Thread);
                return Process.GetProcessById(processInfo.ProcessId);
            }
            finally
            {
                CloseHandle(hAdminToken);
            }
        }
Example #2
0
        public static Process Start(int sessionId, string applicationName, string commandLine)
        {
            var hToken = IntPtr.Zero;

            if (WTSQueryUserToken(sessionId, ref hToken) == 0)
            {
                throw new Win32Exception();
            }

            var hAdminToken = hToken;

            if (UserAccountControlExists)
            {
                var linked = new TOKEN_LINKED_TOKEN();
                int returned;
                if (
                    GetTokenInformation(hToken, TokenInformationClass.TokenLinkedToken, ref linked,
                                        Marshal.SizeOf(linked), out returned) == 0)
                {
                    throw new Win32Exception();
                }

                var hLinkedToken = linked.LinkedToken;
                if (
                    DuplicateTokenEx(hLinkedToken, 0, IntPtr.Zero, SecurityImpersonationLevel.SecurityImpersonation,
                                     TokenType.TokenPrimary, out hAdminToken) == 0)
                {
                    throw new Win32Exception();
                }

                CloseHandle(hLinkedToken);
                CloseHandle(hToken);
            }

            try
            {
                var commandLineText = new StringBuilder(string.Format("\"{0}\" {1}", applicationName, commandLine));
                var info            = new StartupInfo();
                info.Cb = Marshal.SizeOf(info);
                ProcessInformation processInfo;

                IntPtr hEnvironment;
                if (CreateEnvironmentBlock(out hEnvironment, hAdminToken, true) == 0)
                {
                    throw new Win32Exception();
                }

                try
                {
                    if (
                        CreateProcessAsUser(hAdminToken, null, commandLineText, IntPtr.Zero, IntPtr.Zero, false,
                                            CreationFlags.UnicodeEnvironment, hEnvironment, null, ref info,
                                            out processInfo) == 0)
                    {
                        throw new Win32Exception();
                    }
                }
                finally
                {
                    DestroyEnvironmentBlock(hEnvironment);
                }

                CloseHandle(processInfo.Process);
                CloseHandle(processInfo.Thread);
                return(Process.GetProcessById(processInfo.ProcessId));
            }
            finally
            {
                CloseHandle(hAdminToken);
            }
        }
 private static extern int CreateProcessAsUser(IntPtr token, string applicationName, StringBuilder commandLine,
                                               IntPtr processAttributes, IntPtr threadAttributes,
                                               bool inheritHandles, CreationFlags creationFlags,
                                               IntPtr environment, string currentDirectory,
                                               ref StartupInfo startupInfo,
                                               out ProcessInformation processInformation);
Example #4
0
 private static extern int CreateProcessAsUser(IntPtr token, string applicationName, StringBuilder commandLine,
                                               IntPtr processAttributes, IntPtr threadAttributes,
                                               bool inheritHandles, CreationFlags creationFlags,
                                               IntPtr environment, string currentDirectory,
                                               ref StartupInfo startupInfo,
                                               out ProcessInformation processInformation);