public static WINSTATIONINFORMATIONW GetWinStationInformation(IntPtr server, int sessionId) { int retLen = 0; WINSTATIONINFORMATIONW wsInfo = new WINSTATIONINFORMATIONW(); WinStationQueryInformation(server, sessionId, (int)WINSTATIONINFOCLASS.WinStationInformation, ref wsInfo, Marshal.SizeOf(typeof(WINSTATIONINFORMATIONW)), ref retLen); return(wsInfo); }
/// <summary> /// Method LoadWinStationInformationProperties. /// </summary> private void LoadWinStationInformationProperties() { WINSTATIONINFORMATIONW winStationInfo = NativeMethodsHelper.GetWinStationInformation(this._server.Handle, this._sessionId); this._windowStationName.Value = winStationInfo.WinStationName; this._connectState.Value = winStationInfo.ConnectState; this._connectTime.Value = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.ConnectTime); this._currentTime.Value = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.CurrentTime); this._disconnectTime.Value = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.DisconnectTime); this._lastInputTime.Value = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.LastInputTime); this._loginTime.Value = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.LoginTime); this._userName.Value = winStationInfo.UserName; this._domainName.Value = winStationInfo.Domain; this._incomingStatistics.Value = new ProtocolStatistics(winStationInfo.ProtocolStatus.Input); this._outgoingStatistics.Value = new ProtocolStatistics(winStationInfo.ProtocolStatus.Output); }
public static TerminalSessionInfo GetSessionInfo(string ServerName, int SessionId) { IntPtr server = IntPtr.Zero; IntPtr buffer = IntPtr.Zero; int bytesReturned; TerminalSessionInfo data = new TerminalSessionInfo(); bool _IsCurrentSessionId = false; bool _IsConsoleSession = false; bool _IsUserSession = false; int currentSessionID = 0; string _NTAccount = String.Empty; if (ServerName == "localhost" || ServerName == String.Empty) { ServerName = Environment.MachineName; } if (ProcessIdToSessionId(GetCurrentProcessId(), ref currentSessionID) == false) { currentSessionID = -1; } // Get all members of the local administrators group bool _IsLocalAdminCheckSuccess = false; List <string> localAdminGroupSidsList = new List <string>(); try { DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + ServerName + ",Computer"); string localAdminGroupName = new SecurityIdentifier("S-1-5-32-544").Translate(typeof(NTAccount)).Value.Split('\\')[1]; DirectoryEntry admGroup = localMachine.Children.Find(localAdminGroupName, "group"); object members = admGroup.Invoke("members", null); foreach (object groupMember in (IEnumerable)members) { DirectoryEntry member = new DirectoryEntry(groupMember); if (member.Name != String.Empty) { localAdminGroupSidsList.Add((new NTAccount(member.Name)).Translate(typeof(SecurityIdentifier)).Value); } } _IsLocalAdminCheckSuccess = true; } catch { } try { server = OpenServer(ServerName); if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.ClientBuildNumber, out buffer, out bytesReturned) == false) { return(data); } int lData = Marshal.ReadInt32(buffer); data.ClientBuildNumber = lData; if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.ClientDirectory, out buffer, out bytesReturned) == false) { return(data); } string strData = Marshal.PtrToStringAnsi(buffer); data.ClientDirectory = strData; if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.ClientName, out buffer, out bytesReturned) == false) { return(data); } strData = Marshal.PtrToStringAnsi(buffer); data.ClientName = strData; if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.ClientProtocolType, out buffer, out bytesReturned) == false) { return(data); } Int16 intData = Marshal.ReadInt16(buffer); if (intData == 2) { strData = "RDP"; data.IsRdpSession = true; } else { strData = ""; data.IsRdpSession = false; } data.ClientProtocolType = strData; if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.ConnectState, out buffer, out bytesReturned) == false) { return(data); } lData = Marshal.ReadInt32(buffer); data.ConnectState = ((WTS_CONNECTSTATE_CLASS)lData).ToString(); if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.SessionId, out buffer, out bytesReturned) == false) { return(data); } lData = Marshal.ReadInt32(buffer); data.SessionId = lData; if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.DomainName, out buffer, out bytesReturned) == false) { return(data); } strData = Marshal.PtrToStringAnsi(buffer).ToUpper(); data.DomainName = strData; if (strData != String.Empty) { _NTAccount = strData; } if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.UserName, out buffer, out bytesReturned) == false) { return(data); } strData = Marshal.PtrToStringAnsi(buffer); data.UserName = strData; if (strData != String.Empty) { data.NTAccount = _NTAccount + "\\" + strData; string _Sid = (new NTAccount(_NTAccount + "\\" + strData)).Translate(typeof(SecurityIdentifier)).Value; data.SID = _Sid; if (_IsLocalAdminCheckSuccess == true) { foreach (string localAdminGroupSid in localAdminGroupSidsList) { if (localAdminGroupSid == _Sid) { data.IsLocalAdmin = true; break; } else { data.IsLocalAdmin = false; } } } } if (WTSQuerySessionInformation(server, SessionId, WTS_INFO_CLASS.SessionName, out buffer, out bytesReturned) == false) { return(data); } strData = Marshal.PtrToStringAnsi(buffer); data.SessionName = strData; if (strData != "Services" && strData != "RDP-Tcp" && data.UserName != String.Empty) { _IsUserSession = true; } data.IsUserSession = _IsUserSession; if (strData == "Console") { _IsConsoleSession = true; } data.IsConsoleSession = _IsConsoleSession; WINSTATIONINFORMATIONW wsInfo = GetWinStationInformation(server, SessionId); DateTime?_loginTime = FileTimeToDateTime(wsInfo.LoginTime); DateTime?_lastInputTime = FileTimeToDateTime(wsInfo.LastInputTime); DateTime?_disconnectTime = FileTimeToDateTime(wsInfo.DisconnectTime); DateTime?_currentTime = FileTimeToDateTime(wsInfo.CurrentTime); TimeSpan?_idleTime = (_currentTime != null && _lastInputTime != null) ? _currentTime.Value - _lastInputTime.Value : TimeSpan.Zero; data.LogonTime = _loginTime; data.IdleTime = _idleTime; data.DisconnectTime = _disconnectTime; if (currentSessionID == SessionId) { _IsCurrentSessionId = true; } data.IsCurrentSession = _IsCurrentSessionId; } finally { WTSFreeMemory(buffer); buffer = IntPtr.Zero; CloseServer(server); } return(data); }
public static extern int WinStationQueryInformation(IntPtr hServer, int sessionId, int information, ref WINSTATIONINFORMATIONW pBuffer, int bufferLength, ref int returnedLength);