/// <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); } }
private void CheckSessionOrDesktopChanged() { try { // Check for GUI process stopped if (mGuiProcess != null && !mGuiProcess.IsRunning) { Log.Write("GUI process has stopped for unknown reason."); TerminateProcess(ref mGuiProcess, ref mGuiMan, false); mDesktopGuiIsOn = ""; } // Check for DESKTOP process stopped if (mDesktopProcess != null && !mDesktopProcess.IsRunning) { Log.Write("DESKTOP process has stopped for unknown reason."); TerminateProcess(ref mDesktopProcess, ref mDesktopMan, false); mDesktopFromWatcher = ""; } // Check for desktop changed if (mGuiProcess != null && mDesktopFromWatcher != mDesktopGuiIsOn && mDesktopFromWatcher != "") { Log.Write("Stopping GUI process because desktop changed from '" + mDesktopGuiIsOn + "' to '" + mDesktopFromWatcher); TerminateProcess(ref mGuiProcess, ref mGuiMan, false); mDesktopGuiIsOn = ""; } // Check for session changed var sessionId = Wts.GetActiveConsoleSessionId(); var domainName = Wts.GetSessionDomainName(sessionId); var userName = Wts.GetSessionUserName(sessionId); if (sessionId != mSessionId || domainName != mDomainName || userName != mUserName) { mSessionId = sessionId; mDomainName = domainName; mUserName = userName; if (mGuiProcess != null || mDesktopProcess != null) { Log.Write("Stopping GUI and DESKTOP processes because id or user changed, id=" + sessionId + ", user="******""; mDesktopGuiIsOn = ""; if (sessionId < 0) { Log.Write("*** No active session, not starting GUI or DESKTOP processes ***"); } } // Ensure processes are running if (mGuiProcess == null && sessionId >= 0 && mDesktopFromWatcher != "") { StartGuiProcess(sessionId); } if (mDesktopProcess == null && sessionId >= 0) { StartDesktopWatcherProcess(sessionId); } } catch (Exception ex) { // TBD: Use event log Log.Write("Error checking session changed", ex); } }