private void UnmarkForWts(MenuItem mi) { InventoryItem ga = mi.DataContext as InventoryItem; Wts.Remove(ga); ga.Wts = false; }
public void CloseServer() { if (_hServer != (IntPtr)0) { Wts.CloseServer(_hServer); _hServer = (IntPtr)0; } }
public bool OpenServer() { _hServer = Wts.OpenServer(_server.ServerName); if (_hServer == (IntPtr)0) { return(false); } return(true); }
/// <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); } }
public IList <RemoteSessionInfo> QuerySessions() { if (_hServer == (IntPtr)0) { throw new Exception("QuerySessions called before OpenServer succeeded"); } if (!Wts.EnumerateSessions(_hServer, 0, 1, out IntPtr pSessionInfo, out int count)) { return(null); } List <RemoteSessionInfo> list = new List <RemoteSessionInfo>(); Wts.SessionInfo sessionInfo = new Wts.SessionInfo(); try { IntPtr intPtr = pSessionInfo; for (int i = 0; i < count; i++) { Marshal.PtrToStructure(intPtr, (object)sessionInfo); intPtr = (IntPtr)((long)intPtr + Marshal.SizeOf((object)sessionInfo)); string text = null; string text2 = null; string text3 = null; Wts.QuerySessionInformation(_hServer, sessionInfo.SessionId, Wts.InfoClass.UserName, out IntPtr pBuffer, out int bytesReturned); text = Marshal.PtrToStringAuto(pBuffer); if (text.Length != 0) { Wts.QuerySessionInformation(_hServer, sessionInfo.SessionId, Wts.InfoClass.DomainName, out pBuffer, out bytesReturned); text2 = Marshal.PtrToStringAuto(pBuffer); Wts.QuerySessionInformation(_hServer, sessionInfo.SessionId, Wts.InfoClass.ClientName, out pBuffer, out bytesReturned); text3 = Marshal.PtrToStringAuto(pBuffer); list.Add(new RemoteSessionInfo { ClientName = text3, DomainName = text2, SessionId = sessionInfo.SessionId, UserName = text, State = sessionInfo.State }); } } return(list); } finally { Wts.FreeMemory(pSessionInfo); } }
private static bool getActiveSessionUserToken(ref IntPtr phUserToken) { var bResult = false; var hImpersonationToken = IntPtr.Zero; var activeSessionId = Wts.INVALID_SESSION_ID; var pSessionInfo = IntPtr.Zero; var sessionCount = 0; // Get a handle to the user access token for the current active session. if (Wts.WTSEnumerateSessions(Wts.WTS_CURRENT_SERVER_HANDLE, 0, 1, ref pSessionInfo, ref sessionCount) != 0) { var arrayElementSize = Marshal.SizeOf(typeof(Wts.WTS_SESSION_INFO)); var current = pSessionInfo; for (var i = 0; i < sessionCount; i++) { var si = (Wts.WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)current, typeof(Wts.WTS_SESSION_INFO)); current += arrayElementSize; if (si.State == Wts.WTS_CONNECTSTATE_CLASS.WTSActive) { activeSessionId = si.SessionID; } } } // If enumerating did not work, fall back to the old method if (activeSessionId == Wts.INVALID_SESSION_ID) { activeSessionId = Wts.WTSGetActiveConsoleSessionId(); } if (Wts.WTSQueryUserToken(activeSessionId, ref hImpersonationToken) != 0) { // Convert the impersonation token to a primary token var sa = new WinApi.SECURITY_ATTRIBUTES(); bResult = Advapi32.DuplicateTokenEx(hImpersonationToken, 0, sa, WinApi.Advapi32.SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, WinApi.Advapi32.TOKEN_TYPE.TokenPrimary, out phUserToken); Kernel32.CloseHandle(hImpersonationToken); } return(bResult); }
async Task ProcessServerCommands() { try { mProcessManager = new ProcessManager(Program.ParamControlPipe, false); await mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CONNECTED); var commandTask = mProcessManager.ReadCommandAsync(); // Poll for desktop change every POLL_FOR_DESKTOP_CHANGE_MS milliseconds, // and close application when COMMAND_CLOSE is received while (true) { var desktop = Wts.GetDesktopName(); if (desktop != mDesktop) { await mProcessManager.SendCommandAsync(desktop); } mDesktop = desktop; if (commandTask.IsCompleted) { var command = commandTask.Result; commandTask = mProcessManager.ReadCommandAsync(); if (command.StartsWith(ProcessManager.COMMAND_CLOSE)) { await mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CLOSING + "@reason=Closed by service"); break; } } commandTask.Wait(POLL_FOR_DESKTOP_CHANGE_MS); } } catch (Exception ex) { try { mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CLOSING + "@reason=Exception: " + ex.Message + ", Stack: " + ex.StackTrace.Replace("\n", "").Replace("\r", "")).Wait(100); } catch { } } }
public bool LogOffSession(int id) { return(Wts.LogOffSession(_hServer, id, wait: true)); }
public bool DisconnectSession(int id) { return(Wts.DisconnectSession(_hServer, id, wait: true)); }
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); } }