Example #1
0
        private static WTS_CLIENT_INFO LoadClientInfoForSession(IntPtr ptrOpenedServer, int active_session)
        {
            int    returned = 0;
            IntPtr str      = IntPtr.Zero;

            WTS_CLIENT_INFO ClientInfo = new WTS_CLIENT_INFO();

            ClientInfo.WTSStationName = "";
            ClientInfo.WTSClientName  = "";
            ClientInfo.Address        = new byte[6];
            ClientInfo.Address[2]     = 0;
            ClientInfo.Address[3]     = 0;
            ClientInfo.Address[4]     = 0;
            ClientInfo.Address[5]     = 0;

            ClientInfo.WTSClientName  = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientName);
            ClientInfo.WTSStationName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSWinStationName);
            ClientInfo.WTSDomainName  = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSDomainName);

            //Get client IP address
            IntPtr addr = System.IntPtr.Zero;

            if (WTSQuerySessionInformation2(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientAddress, ref addr, ref returned) == true)
            {
                _WTS_CLIENT_ADDRESS obj = new _WTS_CLIENT_ADDRESS();
                obj = (_WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(addr, obj.GetType());
                ClientInfo.Address[2] = obj.Address[2];
                ClientInfo.Address[3] = obj.Address[3];
                ClientInfo.Address[4] = obj.Address[4];
                ClientInfo.Address[5] = obj.Address[5];
            }
            return(ClientInfo);
        }
Example #2
0
        /// <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);
        }
        private static WTS_CLIENT_INFO LoadClientInfoForSession(IntPtr ptrOpenedServer, int active_session)
        {
            int returned = 0;
            IntPtr str = IntPtr.Zero;

            WTS_CLIENT_INFO ClientInfo = new WTS_CLIENT_INFO
                                             {
                                                 WTSStationName = "",
                                                 WTSClientName = "",
                                                 Address = new byte[6]
                                             };

            ClientInfo.Address[2] = 0;
            ClientInfo.Address[3] = 0;
            ClientInfo.Address[4] = 0;
            ClientInfo.Address[5] = 0;

            ClientInfo.WTSClientName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientName);
            ClientInfo.WTSStationName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSWinStationName);
            ClientInfo.WTSDomainName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSDomainName);

            //Get client IP address 
            IntPtr addr = IntPtr.Zero;

            if (WTSQuerySessionInformation2(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientAddress, ref addr,
                                            ref returned))
            {
                _WTS_CLIENT_ADDRESS obj = new _WTS_CLIENT_ADDRESS();
                obj = (_WTS_CLIENT_ADDRESS) Marshal.PtrToStructure(addr, obj.GetType());
                ClientInfo.Address[2] = obj.Address[2];
                ClientInfo.Address[3] = obj.Address[3];
                ClientInfo.Address[4] = obj.Address[4];
                ClientInfo.Address[5] = obj.Address[5];
            }

            return ClientInfo;
        }
        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);
            }
        }