public static uint GetRDPSession() { IntPtr ppSessionInfo = IntPtr.Zero; Int32 count = 0; Int32 retval = WTSAPI32.WTSEnumerateSessions(WTSAPI32.WTS_CURRENT_SERVER_HANDLE, 0, 1, ref ppSessionInfo, ref count); Int32 dataSize = Marshal.SizeOf(typeof(WTSAPI32.WTS_SESSION_INFO)); var sessList = new List <WTSAPI32.WTS_SESSION_INFO>(); Int64 current = (Int64)ppSessionInfo; if (retval != 0) { for (int i = 0; i < count; i++) { WTSAPI32.WTS_SESSION_INFO sessInf = (WTSAPI32.WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTSAPI32.WTS_SESSION_INFO)); current += dataSize; sessList.Add(sessInf); } } uint retVal = 0; var rdpSession = sessList.Find(ses => ses.pWinStationName.ToLower().Contains("rdp") && ses.State == 0); if (sessList.Exists(ses => ses.pWinStationName.ToLower().Contains("rdp") && ses.State == 0)) { retVal = (uint)rdpSession.SessionID; } return(retVal); }
private void EnumerateSessionsHandler(TcpSocketSaeaSession session) { lock (_lock) { var sessions = Win32Interop.EnumerateSessions() .Select(c => new SessionItem() { UserName = WTSAPI32.GetUserNameBySessionId(c.SessionID), SessionState = (int)c.State, WindowStationName = c.pWinStationName, SessionId = c.SessionID, HasUserProcess = _userProcessSessionIdList.FindIndex(i => i.SessionId == c.SessionID) > -1 ? true : false }) .ToArray(); LogHelper.DebugWriteLog("Session-Count:" + sessions.Count()); var data = MessageHelper.CopyMessageHeadTo(TrunkMessageHead.C_SessionItems, new SessionStatusPack() { Sessions = sessions }); session.SendAsync(data); } }