/// <summary> /// Get a List of all Sessions on a server and IP address. /// </summary> /// <param name="ServerName"></param> /// <returns></returns> public static List <String> ListSessions(String ServerName) { IntPtr server = IntPtr.Zero; List <String> ret = new List <string>(); 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++) { WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO)); current += dataSize; #region OTsSession uint returned = 0;; TsSession oTsSession = new TsSession(); //IP address IntPtr addr = IntPtr.Zero; if (WTSQuerySessionInformation(server, si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out addr, out returned) == true) { _WTS_CLIENT_ADDRESS obj = new _WTS_CLIENT_ADDRESS(); obj = (_WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(addr, obj.GetType()); oTsSession.IpAddress = obj.Address[2] + "." + obj.Address[3] + "." + obj.Address[4] + "." + obj.Address[5]; } #endregion ret.Add(si.SessionID + " " + si.State + " " + si.pWinStationName + " " + oTsSession.IpAddress); } WTSFreeMemory(ppSessionInfo); } } finally { CloseServer(server); } return(ret); }
public static IEnumerable <UserInfo> GetUsers(String ServerName) { IntPtr serverHandle = IntPtr.Zero; List <String> resultList = new List <string>(); serverHandle = OpenServer(ServerName); try { IntPtr SessionInfoPtr = IntPtr.Zero; IntPtr userPtr = IntPtr.Zero; IntPtr domainPtr = IntPtr.Zero; //IntPtr clientnamePtr = IntPtr.Zero; TsSession oTsSession = new TsSession(); IntPtr addrPtr = IntPtr.Zero; uint returned = 0;; Int32 sessionCount = 0; Int32 retVal = WTSEnumerateSessions(serverHandle, 0, 1, ref SessionInfoPtr, ref sessionCount); Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); Int32 currentSession = (int)SessionInfoPtr; uint bytes = 0; if (retVal != 0) { for (int i = 0; i < sessionCount; i++) { WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(WTS_SESSION_INFO)); currentSession += dataSize; WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSUserName, out userPtr, out bytes); WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSDomainName, out domainPtr, out bytes); //WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out clientnamePtr, out bytes); if (WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out addrPtr, out returned) == true) { _WTS_CLIENT_ADDRESS obj = new _WTS_CLIENT_ADDRESS(); obj = (_WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(addrPtr, obj.GetType()); oTsSession.IpAddress = obj.Address[2] + "." + obj.Address[3] + "." + obj.Address[4] + "." + obj.Address[5]; } yield return(new UserInfo { Domain = Marshal.PtrToStringAnsi(domainPtr), User = Marshal.PtrToStringAnsi(userPtr), ClientIP = oTsSession.IpAddress, SessionID = si.SessionID, ClientName = si.pWinStationName }); WTSFreeMemory(userPtr); WTSFreeMemory(domainPtr); WTSFreeMemory(addrPtr); } WTSFreeMemory(SessionInfoPtr); } } finally { CloseServer(serverHandle); } }