public static List<SessionInfo> ListSessions(string serverName, string userName, string domainName, string clientName, WTS_CONNECTSTATE_CLASS? state) { IntPtr server = IntPtr.Zero; List<SessionInfo> sessions = new List<SessionInfo>(); server = OpenServer(serverName); try { IntPtr ppSessionInfo = IntPtr.Zero; Int32 count = 0; Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count); Int32 dataSize = Marshal.SizeOf(typeof (WTS_SESSION_INFO)); Int32 current = (int) ppSessionInfo; if (retval != 0) { for (int i = 0; i < count; i++) { SessionInfo sessionInfo = new SessionInfo(); WTS_SESSION_INFO si = (WTS_SESSION_INFO) Marshal.PtrToStructure((IntPtr) current, typeof (WTS_SESSION_INFO)); current += dataSize; sessionInfo.Id = si.SessionID; sessionInfo.UserName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSUserName); sessionInfo.DomainName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSDomainName); sessionInfo.ClientName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSClientName); sessionInfo.State = si.State; if (userName != null || domainName != null || clientName != null || state != null) //In this case, the caller is asking to return only matching sessions { if (userName != null && !String.Equals(userName, sessionInfo.UserName, StringComparison.CurrentCultureIgnoreCase)) continue; //Not matching if (clientName != null && !String.Equals(clientName, sessionInfo.ClientName, StringComparison.CurrentCultureIgnoreCase)) continue; //Not matching if (domainName != null && !String.Equals(domainName, sessionInfo.DomainName, StringComparison.CurrentCultureIgnoreCase)) continue; //Not matching if (state != null && sessionInfo.State != state.Value) continue; } sessions.Add(sessionInfo); } WTSFreeMemory(ppSessionInfo); } } finally { CloseServer(server); } return sessions; }
public static List <SessionInfo> ListSessions(string serverName, string userName, string domainName, string clientName, WTS_CONNECTSTATE_CLASS?state) { IntPtr server = IntPtr.Zero; List <SessionInfo> sessions = new List <SessionInfo>(); server = OpenServer(serverName); try { IntPtr ppSessionInfo = IntPtr.Zero; Int32 count = 0; Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count); Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); Int32 current = (int)ppSessionInfo; if (retval != 0) { for (int i = 0; i < count; i++) { SessionInfo sessionInfo = new SessionInfo(); WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)current, typeof(WTS_SESSION_INFO)); current += dataSize; sessionInfo.Id = si.SessionID; sessionInfo.UserName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSUserName); sessionInfo.DomainName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSDomainName); sessionInfo.ClientName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSClientName); sessionInfo.State = si.State; if (userName != null || domainName != null || clientName != null || state != null) //In this case, the caller is asking to return only matching sessions { if (userName != null && !String.Equals(userName, sessionInfo.UserName, StringComparison.CurrentCultureIgnoreCase)) { continue; //Not matching } if (clientName != null && !String.Equals(clientName, sessionInfo.ClientName, StringComparison.CurrentCultureIgnoreCase)) { continue; //Not matching } if (domainName != null && !String.Equals(domainName, sessionInfo.DomainName, StringComparison.CurrentCultureIgnoreCase)) { continue; //Not matching } if (state != null && sessionInfo.State != state.Value) { continue; } } sessions.Add(sessionInfo); } WTSFreeMemory(ppSessionInfo); } } finally { CloseServer(server); } return(sessions); }