void StartGuiProcess(int sessionId) { TerminateProcess(ref mGuiProcess, ref mGuiMan, false); mDesktopGuiIsOn = ""; var desktop = mDesktopFromWatcher; mDesktopGuiIsOn = desktop; var pipeName = "OpenViewtop_gui_" + Util.GenerateRandomId(); // When on the default desktop, run as the user so the clipboard works properly. // For other desktops we need to run as system so the GUI has permission to copy the screen. // TBD: Seems like there should be a better way to do this WtsProcessType processType = desktop.ToLower() != DEFAULT_DESKTOP.ToLower() || sessionId == 0 ? WtsProcessType.System : WtsProcessType.UserFallbackToSystem; processType |= WtsProcessType.Admin | WtsProcessType.UIAccess; // Mange process communications Log.Write("*** Starting GUI: Session=" + sessionId + ", User="******", Desktop=" + desktop + ", Pipe=" + pipeName); mGuiMan = new ProcessManager(pipeName, true); Task.Run(() => { ProcessRemoteCommunications(mGuiMan, "GUI"); }); mGuiProcess = WtsProcess.StartInSession( sessionId, processType, System.Windows.Forms.Application.ExecutablePath, Program.PARAM_GUI + " " + Program.PARAM_CONTROL_PIPE + " " + pipeName + " " + Program.PARAM_DESKTOP + " " + desktop, desktop); }
/// <summary> /// Don't launch using Process.Start, or else it runs as "System" /// </summary> void ProcessStartAsLoggedOnUser(string fileName) { try { // Launch from helper task running as the user, also with the user environment // NOTE: This fails unless it's running in the system accont WtsProcess.StartInSession(Wts.GetActiveConsoleSessionId(), WtsProcessType.User, Application.ExecutablePath, Program.PARAM_START_BROWSER + " \"" + fileName + "\"").Dispose(); } catch { // NOTE: New versions run in as the logged on user and the above code fails. Process.Start(fileName); } }
void StartDesktopWatcherProcess(int sessionId) { TerminateProcess(ref mDesktopProcess, ref mDesktopMan, false); mDesktopFromWatcher = ""; // Create new process var pipeName = "OpenViewtop_desktop_" + Util.GenerateRandomId(); // Manage communications Log.Write("*** Starting DESKTOP: Session=" + sessionId + ", User="******", Pipe=" + pipeName); mDesktopMan = new ProcessManager(pipeName, true); Task.Run(() => { ProcessRemoteCommunications(mDesktopMan, "DESKTOP"); }); mDesktopProcess = WtsProcess.StartInSession( sessionId, WtsProcessType.System, System.Windows.Forms.Application.ExecutablePath, Program.PARAM_WATCH_DESKTOP + " " + Program.PARAM_CONTROL_PIPE + " " + pipeName); }