private void RefreshThread_DoWork(object sender, DoWorkEventArgs e) { WTSUniqueSessionIdList valid_session_ids = new WTSUniqueSessionIdList(); foreach (string server in this.WTSServerHostnames) { IntPtr hServer = IntPtr.Zero; try { IntPtr SessionInfoPtr = IntPtr.Zero; Int32 sessionCount = 0; Int32 dataSize = Marshal.SizeOf(typeof(Jrfc.NativeMethods.WTS_SESSION_INFO)); hServer = Jrfc.NativeMethods.WTSOpenServer(server); Int32 retVal = Jrfc.NativeMethods.WTSEnumerateSessions(hServer, 0, 1, ref SessionInfoPtr, ref sessionCount); if (retVal != 0) { Int32 currentSession = (int)SessionInfoPtr; for (int i = 0; i < sessionCount; i++) { Jrfc.NativeMethods.WTS_SESSION_INFO si = (Jrfc.NativeMethods.WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(Jrfc.NativeMethods.WTS_SESSION_INFO)); currentSession += dataSize; Jrfc.Wts.WTSSession session = new Jrfc.Wts.WTSSession(hServer, server, si); bool filter = false; if (session.State == NativeMethods.WTS_CONNECTSTATE_CLASS.WTSListen) { filter = true; } else if (session.State == NativeMethods.WTS_CONNECTSTATE_CLASS.WTSDisconnected && string.IsNullOrWhiteSpace(session.Username)) { filter = true; } if (!filter) { this.AddOrUpdate(session); valid_session_ids.Add(new WTSUniqueSessionId(server, session.SessionId)); // keep a list of existing sessions } } Jrfc.NativeMethods.WTSFreeMemory(SessionInfoPtr); } } catch (System.Exception x) { Jrfc.Exception.HandleException(x); } finally { if (hServer != IntPtr.Zero) { Jrfc.NativeMethods.WTSCloseServer(hServer); } } } DeleteAllSessionIdsNotInList(valid_session_ids); // delete (old) sessions that are in the currect session list, // but were not in the existing sessions just retreived from the // RDS server. Presumably any session not in the valid_session_ids // list no longer exists. this.m_RefreshThread.ReportProgress(100); }
//public WTSSession(string _Hostname, Jrfc.WinApi.WTS_SESSION_INFO _si) //{ // this.SessionId = _si.SessionId; // this.WinStationname = Marshal.PtrToStringAnsi(this.WtsSessionInfo.pWinStationName); // this.UniqueSessionId = new WTSUniqueSessionId(_Hostname, _si.SessionId); // this.State = _si.State; //} public WTSSession(IntPtr _ServerHandle, string _Hostname, Jrfc.NativeMethods.WTS_SESSION_INFO _si) { this.SessionId = _si.SessionId; this.State = _si.State; this.WinStationName = (string)Marshal.PtrToStringAnsi(_si.pWinStationName); this.UniqueSessionId = new WTSUniqueSessionId(_Hostname, _si.SessionId); this.Username = (string)this.QuerySessionInformation(_ServerHandle, Jrfc.NativeMethods.WTS_INFO_CLASS.WTSUserName); this.ClientHostname = (string)this.QuerySessionInformation(_ServerHandle, Jrfc.NativeMethods.WTS_INFO_CLASS.WTSClientName); this.DomainName = (string)this.QuerySessionInformation(_ServerHandle, Jrfc.NativeMethods.WTS_INFO_CLASS.WTSDomainName); this.WtsClientAddress = (Jrfc.NativeMethods.WTS_CLIENT_ADDRESS) this.QuerySessionInformation(_ServerHandle, Jrfc.NativeMethods.WTS_INFO_CLASS.WTSClientAddress); }