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); } }
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 GetTokenInformation(IntPtr token, TokenInformationClass infoClass, ref TOKEN_LINKED_TOKEN buffer, int bufferLength, out int returnLength);