public SendMessageWindow(RemoteLogonSession windowsUser) { InitializeComponent(); // Set initial focus to text box. Loaded += (sender, e) => MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); _WindowsUser = windowsUser; tbBody.Text = $"Enter a message for {windowsUser.Username} on {RemoteLogonSession.ComputerName}."; }
public static List <RemoteLogonSession> GetWindowsUsers(IntPtr server) { var windowsUsers = new List <RemoteLogonSession>(); IntPtr buffer = IntPtr.Zero; int count = 0; try { int retval = WTSEnumerateSessions(server, 0, 1, ref buffer, ref count); int dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); Int64 current = (int)buffer; if (retval != 0) { for (int i = 0; i < count; i++) { var windowsUser = new RemoteLogonSession(); var bufferTwo = IntPtr.Zero; uint bytesReturned = 0; WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)current, typeof(WTS_SESSION_INFO)); current += dataSize; windowsUser.SessionId = Convert.ToUInt32(si.SessionID); try { // Get the username of the Terminal Services user. if (WTSQuerySessionInformation(server, si.SessionID, WTS_INFO_CLASS.WTSUserName, out buffer, out bytesReturned) == true) { windowsUser.Username = Marshal.PtrToStringAnsi(buffer).Trim(); } if (string.IsNullOrWhiteSpace(windowsUser.Username)) { continue; } // Get the connection state of the Terminal Services user. if (si.State == WTS_CONNECTSTATE_CLASS.WTSDisconnected) { windowsUser.IsDisconnected = true; } // Get the IP address of the Terminal Services user. if (WTSQuerySessionInformation(server, si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out buffer, out bytesReturned) == true) { var clientAddress = new WTS_CLIENT_ADDRESS(); clientAddress = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(buffer, clientAddress.GetType()); windowsUser.IpAddress = clientAddress.Address[2] + "." + clientAddress.Address[3] + "." + clientAddress.Address[4] + "." + clientAddress.Address[5]; } windowsUsers.Add(windowsUser); } finally { WTSFreeMemory(bufferTwo); } } } } finally { WTSFreeMemory(buffer); } return(windowsUsers); }
private void bgThread_LogoffUser(object sender, DoWorkEventArgs e) { // Background thread to logoff a user. e.Result = RemoteLogonSession.LogoffUser(e.Argument as RemoteLogonSession); }
private void bgThread_GetLogonSessions(object sender, DoWorkEventArgs e) { e.Result = RemoteLogonSession.GetLogonSessions(); }