示例#1
0
        public static uint GetRDPSession()
        {
            var    consoleSessionId  = Kernel32.WTSGetActiveConsoleSessionId();
            uint   activeSessionId   = 0;
            IntPtr ppSessionInfo     = IntPtr.Zero;
            var    count             = 0;
            var    enumSessionResult = WTSAPI32.WTSEnumerateSessions(WTSAPI32.WTS_CURRENT_SERVER_HANDLE, 0, 1, ref ppSessionInfo, ref count);
            var    dataSize          = Marshal.SizeOf(typeof(WTSAPI32.WTS_SESSION_INFO));
            var    current           = ppSessionInfo;

            if (enumSessionResult != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    WTSAPI32.WTS_SESSION_INFO sessionInfo = (WTSAPI32.WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTSAPI32.WTS_SESSION_INFO));
                    current += dataSize;
                    if (sessionInfo.State == WTSAPI32.WTS_CONNECTSTATE_CLASS.WTSActive && sessionInfo.SessionID != consoleSessionId)
                    {
                        activeSessionId = sessionInfo.SessionID;
                    }
                }
            }

            return(activeSessionId);
        }
示例#2
0
        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);
        }
示例#3
0
        public static string GetUsernameFromSessionId(uint sessionId)
        {
            var username = string.Empty;

            if (WTSAPI32.WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTSAPI32.WTS_INFO_CLASS.WTSUserName, out var buffer, out var strLen) && strLen > 1)
            {
                username = Marshal.PtrToStringAnsi(buffer);
                WTSAPI32.WTSFreeMemory(buffer);
            }

            return(username);
        }
示例#4
0
        public static List <WindowsSession> GetActiveSessions()
        {
            var sessions         = new List <WindowsSession>();
            var consoleSessionId = Kernel32.WTSGetActiveConsoleSessionId();

            sessions.Add(new WindowsSession()
            {
                ID       = consoleSessionId,
                Type     = SessionType.Console,
                Name     = "Console",
                Username = GetUsernameFromSessionId(consoleSessionId)
            });

            IntPtr ppSessionInfo     = IntPtr.Zero;
            var    count             = 0;
            var    enumSessionResult = WTSAPI32.WTSEnumerateSessions(WTSAPI32.WTS_CURRENT_SERVER_HANDLE, 0, 1, ref ppSessionInfo, ref count);
            var    dataSize          = Marshal.SizeOf(typeof(WTSAPI32.WTS_SESSION_INFO));
            var    current           = ppSessionInfo;

            if (enumSessionResult != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    WTSAPI32.WTS_SESSION_INFO sessionInfo = (WTSAPI32.WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTSAPI32.WTS_SESSION_INFO));
                    current += dataSize;
                    if (sessionInfo.State == WTSAPI32.WTS_CONNECTSTATE_CLASS.WTSActive && sessionInfo.SessionID != consoleSessionId)
                    {
                        sessions.Add(new WindowsSession()
                        {
                            ID       = sessionInfo.SessionID,
                            Name     = sessionInfo.pWinStationName,
                            Type     = SessionType.RDP,
                            Username = GetUsernameFromSessionId(sessionInfo.SessionID)
                        });
                    }
                }
            }

            return(sessions);
        }