Пример #1
0
        public static TerminalSessionInfo GetSessionInfo(string ServerName, int SessionId)
        {
            IntPtr server = IntPtr.Zero;

            server = OpenServer(ServerName);
            System.IntPtr       buffer = IntPtr.Zero;
            uint                bytesReturned;
            TerminalSessionInfo data = new TerminalSessionInfo();

            try
            {
                bool worked = WTSQuerySessionInformation(server, SessionId,
                                                         WTS_INFO_CLASS.ApplicationName, out buffer, out bytesReturned);

                if (!worked)
                {
                    return(data);
                }

                string strData = Marshal.PtrToStringAnsi(buffer);
                data.ApplicationName = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientAddress, out buffer, out bytesReturned);

                if (!worked)
                {
                    return(data);
                }

                WTS_CLIENT_ADDRESS si = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure((System.IntPtr)buffer, typeof(WTS_CLIENT_ADDRESS));
                data.ClientAddress = si;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientBuildNumber, out buffer, out bytesReturned);

                if (!worked)
                {
                    return(data);
                }

                int lData = Marshal.ReadInt32(buffer);
                data.ClientBuildNumber = lData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientDirectory, out buffer, out bytesReturned);

                if (!worked)
                {
                    return(data);
                }

                strData = Marshal.PtrToStringAnsi(buffer);
                data.ClientDirectory = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientDisplay, out buffer, out bytesReturned);

                if (!worked)
                {
                    return(data);
                }

                WTS_CLIENT_DISPLAY cd = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure((System.IntPtr)buffer, typeof(WTS_CLIENT_DISPLAY));
                data.ClientDisplay = cd;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientHardwareId, out buffer, out bytesReturned);

                if (!worked)
                {
                    return(data);
                }

                lData = Marshal.ReadInt32(buffer);
                data.ClientHardwareId = lData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientName, out buffer, out bytesReturned);
                strData         = Marshal.PtrToStringAnsi(buffer);
                data.ClientName = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientProductId, out buffer, out bytesReturned);
                Int16 intData = Marshal.ReadInt16(buffer);
                data.ClientProductId = intData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ClientProtocolType, out buffer, out bytesReturned);
                intData = Marshal.ReadInt16(buffer);
                data.ClientProtocolType = intData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.ConnectState, out buffer, out bytesReturned);
                lData             = Marshal.ReadInt32(buffer);
                data.ConnectState = (WTS_CONNECTSTATE_CLASS)Enum.ToObject(typeof(WTS_CONNECTSTATE_CLASS), lData);

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.DomainName, out buffer, out bytesReturned);
                strData         = Marshal.PtrToStringAnsi(buffer);
                data.DomainName = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.InitialProgram, out buffer, out bytesReturned);
                strData             = Marshal.PtrToStringAnsi(buffer);
                data.InitialProgram = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.OEMId, out buffer, out bytesReturned);
                strData    = Marshal.PtrToStringAnsi(buffer);
                data.OEMId = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.SessionId, out buffer, out bytesReturned);
                lData          = Marshal.ReadInt32(buffer);
                data.SessionId = lData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.UserName, out buffer, out bytesReturned);
                strData       = Marshal.PtrToStringAnsi(buffer);
                data.UserName = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.WinStationName, out buffer, out bytesReturned);
                strData             = Marshal.PtrToStringAnsi(buffer);
                data.WinStationName = strData;

                worked = WTSQuerySessionInformation(server, SessionId,
                                                    WTS_INFO_CLASS.WorkingDirectory, out buffer, out bytesReturned);
                strData = Marshal.PtrToStringAnsi(buffer);
                data.WorkingDirectory = strData;
            }
            finally
            {
                WTSFreeMemory(buffer);
                buffer = IntPtr.Zero;
                CloseServer(server);
            }

            return(data);
        }
Пример #2
0
        public override IEnumerable <CommandDTOBase?> Execute(string[] args)
        {
            // adapted from http://www.pinvoke.net/default.aspx/wtsapi32.wtsenumeratesessions
            string computerName = "localhost";

            if (!String.IsNullOrEmpty(ThisRunTime.ComputerName))
            {
                computerName = ThisRunTime.ComputerName;
            }
            else if (args.Length == 1)
            {
                computerName = args[0];
            }

            var server = WTSOpenServer(computerName);

            try
            {
                var ppSessionInfo = IntPtr.Zero;
                var count         = 0;
                var level         = 1;
                var retval        = WTSEnumerateSessionsEx(server, ref level, 0, ref ppSessionInfo, ref count);
                var dataSize      = Marshal.SizeOf(typeof(WTS_SESSION_INFO_1));
                var current       = (long)ppSessionInfo;

                if (retval != 0)
                {
                    for (var i = 0; i < count; i++)
                    {
                        var si = (WTS_SESSION_INFO_1)Marshal.PtrToStructure((IntPtr)current, typeof(WTS_SESSION_INFO_1));
                        current += dataSize;

                        // Now use WTSQuerySessionInformation to get the remote IP (if any) for the connection


                        WTSQuerySessionInformation(server, (uint)si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out var addressPtr, out _);
                        var    address  = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(addressPtr, typeof(WTS_CLIENT_ADDRESS));
                        string sourceIp = null;
                        if (address.Address[2] != 0)
                        {
                            sourceIp = $"{address.Address[2]}.{address.Address[3]}.{address.Address[4]}.{address.Address[5]}";
                        }
                        WTSFreeMemory(addressPtr);

                        // Get Source Hostname
                        WTSQuerySessionInformation(server, (uint)si.SessionID, WTS_INFO_CLASS.WTSClientName, out var hostnamePtr, out _);
                        string sourceHostname = Marshal.PtrToStringAuto(hostnamePtr);
                        WTSFreeMemory(hostnamePtr);

                        //Get Source Display

                        WTSQuerySessionInformation(server, (uint)si.SessionID, WTS_INFO_CLASS.WTSClientDisplay, out var displayPtr, out _);
                        WTS_CLIENT_DISPLAY sourceDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(displayPtr, typeof(WTS_CLIENT_DISPLAY));

                        string sourceResolution = "";
                        if (sourceDisplay.HorizontalResolution != 0)
                        {
                            sourceResolution = String.Format("{0}x{1} @ {2} bits per pixel", sourceDisplay.HorizontalResolution, sourceDisplay.VerticalResolution, sourceDisplay.ColorDepth);
                        }
                        WTSFreeMemory(displayPtr);

                        // Get Client Build
                        WTSQuerySessionInformation(server, (uint)si.SessionID, WTS_INFO_CLASS.WTSClientBuildNumber, out var clientBuildNumberPtr, out _);
                        int    sourceBuildNumber = Marshal.ReadInt32(clientBuildNumberPtr);
                        string sourceClientBuild = "";
                        if (sourceBuildNumber != 0)
                        {
                            sourceClientBuild = sourceBuildNumber.ToString();
                        }

                        WTSFreeMemory(clientBuildNumberPtr);

                        // Get the last input time to calculate idle time
                        string idleTimeString = "";

                        // Vista / Windows Server 2008+. - Previous versions we need to implement WINSTATIONINFORMATION - TODO
                        if (Environment.OSVersion.Version >= new Version(6, 0))
                        {
                            WTSQuerySessionInformation(server, (uint)si.SessionID, WTS_INFO_CLASS.WTSSessionInfo, out var sessionInfoPtr, out _);
                            WTSINFO sessionInfo = (WTSINFO)Marshal.PtrToStructure(sessionInfoPtr, typeof(WTSINFO));

                            long lastInput = sessionInfo.LastInputTime;
                            if (lastInput != 0)
                            {
                                DateTime lastInputDt = DateTime.FromFileTimeUtc(lastInput);
                                TimeSpan idleTime    = DateTime.Now - lastInputDt;
                                idleTimeString = String.Format("{0} minute{1}", idleTime.Minutes, idleTime.Minutes == 1 ? "" : "s");
                            }
                            WTSFreeMemory(sessionInfoPtr);
                        }

                        yield return(new RDPSessionsDTO()
                        {
                            SessionID = si.SessionID,
                            SessionName = si.pSessionName,
                            UserName = si.pUserName,
                            DomainName = si.pDomainName,
                            State = si.State,
                            IdleTime = idleTimeString,
                            SourceIp = sourceIp,
                            SourceHostname = sourceHostname,
                            SourceResolution = sourceResolution,
                            SourceClientBuild = sourceClientBuild,
                        });
                    }

                    WTSFreeMemory(ppSessionInfo);
                }
            }
            finally
            {
                WTSCloseServer(server);
            }
        }
Пример #3
0
 public static extern bool WTSQuerySessionInformation(int ServerHandle, int SessionID,
     WTS_INFO_CLASS InfoClass,
     out WTS_CLIENT_DISPLAY[] Buffer,
     out int BytesReturned);
Пример #4
0
 public ClientDisplay(WTS_CLIENT_DISPLAY clientDisplay)
 {
     _horizontalResolution = clientDisplay.HorizontalResolution;
     _verticalResolution = clientDisplay.VerticalResolution;
     _bitsPerPixel = GetBitsPerPixel(clientDisplay.ColorDepth);
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientDisplay" /> class.
 /// </summary>
 /// <param name="clientDisplay">WTS_CLIENT_DISPLAY instance.</param>
 internal ClientDisplay(WTS_CLIENT_DISPLAY clientDisplay)
 {
     this._horizontalResolution = clientDisplay.HorizontalResolution;
     this._verticalResolution   = clientDisplay.VerticalResolution;
     this._bitsPerPixel         = GetBitsPerPixel(clientDisplay.ColorDepth);
 }
Пример #6
0
        public List <sessioninformation> getsessionlist()
        {
            List <sessioninformation> info = new List <sessioninformation>();
            IntPtr pServer   = IntPtr.Zero;
            string sUserName = string.Empty;
            string sDomain   = string.Empty;
            string sClientApplicationDirectory = string.Empty;
            string sIPAddress = string.Empty;

            WTS_CLIENT_ADDRESS oClientAddres  = new WTS_CLIENT_ADDRESS();
            WTS_CLIENT_DISPLAY oClientDisplay = new WTS_CLIENT_DISPLAY();

            IntPtr pSessionInfo = IntPtr.Zero;

            int iCount       = 0;
            int iReturnValue = WTSEnumerateSessions
                                   (pServer, 0, 1, ref pSessionInfo, ref iCount);
            int iDataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

            int iCurrent = (int)pSessionInfo;

            if (iReturnValue != 0)
            {
                //Go to all sessions
                for (int i = 0; i < iCount; i++)
                {
                    sessioninformation sess = new sessioninformation();

                    WTS_SESSION_INFO oSessionInfo =
                        (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)iCurrent,
                                                                 typeof(WTS_SESSION_INFO));
                    iCurrent += iDataSize;

                    uint iReturned = 0;

                    //Get the IP address of the Terminal Services User
                    IntPtr pAddress = IntPtr.Zero;
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientAddress,
                                                   out pAddress, out iReturned) == true)
                    {
                        oClientAddres = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure
                                            (pAddress, oClientAddres.GetType());
                        sIPAddress = oClientAddres.bAddress[2] + "." +
                                     oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4]
                                     + "." + oClientAddres.bAddress[5];
                    }
                    //Get the User Name of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSUserName,
                                                   out pAddress, out iReturned) == true)
                    {
                        sUserName = Marshal.PtrToStringAnsi(pAddress);
                    }
                    //Get the Domain Name of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSDomainName,
                                                   out pAddress, out iReturned) == true)
                    {
                        sDomain = Marshal.PtrToStringAnsi(pAddress);
                    }
                    //Get the Display Information  of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDisplay,
                                                   out pAddress, out iReturned) == true)
                    {
                        oClientDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure
                                             (pAddress, oClientDisplay.GetType());
                    }
                    //Get the Application Directory of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID,
                                                   WTS_INFO_CLASS.WTSClientDirectory, out pAddress, out iReturned) == true)
                    {
                        sClientApplicationDirectory = Marshal.PtrToStringAnsi(pAddress);
                    }

                    sess.sessionid    = oSessionInfo.iSessionID;
                    sess.sessionname  = oSessionInfo.sWinsWorkstationName;
                    sess.sessionstate = oSessionInfo.oState;
                    sess.ipaddress    = sIPAddress;
                    sess.username     = sUserName;
                    sess.domain       = sDomain;
                    sess.resolution_x = oClientDisplay.iHorizontalResolution;
                    sess.resolution_y = oClientDisplay.iVerticalResolution;

                    info.Add(sess);
                }

                WTSFreeMemory(pSessionInfo);
            }

            return(info);
        }
Пример #7
0
        private static void GetAllSessions()
        {
            IntPtr pServer = IntPtr.Zero;
            string sUserName = string.Empty;
            string sDomain = string.Empty;
            string sClientApplicationDirectory = string.Empty;
            string sIPAddress = string.Empty;

            WTS_CLIENT_ADDRESS oClientAddres = new WTS_CLIENT_ADDRESS();
            WTS_CLIENT_DISPLAY oClientDisplay = new WTS_CLIENT_DISPLAY();

            IntPtr pSessionInfo = IntPtr.Zero;

            int iCount = 0;
            int iReturnValue = WTSEnumerateSessions
                (pServer, 0, 1, ref pSessionInfo, ref iCount);
            int iDataSize = Marshal.SizeOf(typeof (WTS_SESSION_INFO));

            int iCurrent = (int) pSessionInfo;

            if (iReturnValue != 0)
            {
                //Go to all sessions
                for (int i = 0; i < iCount; i++)
                {
                    WTS_SESSION_INFO oSessionInfo =
                        (WTS_SESSION_INFO) Marshal.PtrToStructure((System.IntPtr) iCurrent,
                                                                  typeof (WTS_SESSION_INFO));
                    iCurrent += iDataSize;

                    uint iReturned = 0;

                    //Get the IP address of the Terminal Services User
                    IntPtr pAddress = IntPtr.Zero;
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientAddress,
                                                   out pAddress, out iReturned) == true)
                    {
                        oClientAddres = (WTS_CLIENT_ADDRESS) Marshal.PtrToStructure
                                                                 (pAddress, oClientAddres.GetType());
                        sIPAddress = oClientAddres.bAddress[2] + "." +
                                     oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4]
                                     + "." + oClientAddres.bAddress[5];
                    }
                    //Get the User Name of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSUserName,
                                                   out pAddress, out iReturned) == true)
                    {
                        sUserName = Marshal.PtrToStringAnsi(pAddress);
                    }
                    //Get the Domain Name of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSDomainName,
                                                   out pAddress, out iReturned) == true)
                    {
                        sDomain = Marshal.PtrToStringAnsi(pAddress);
                    }
                    //Get the Display Information  of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer,
                                                   oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDisplay,
                                                   out pAddress, out iReturned) == true)
                    {
                        oClientDisplay = (WTS_CLIENT_DISPLAY) Marshal.PtrToStructure
                                                                  (pAddress, oClientDisplay.GetType());
                    }
                    //Get the Application Directory of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID,
                                                   WTS_INFO_CLASS.WTSClientDirectory, out pAddress, out iReturned) ==
                        true)
                    {
                        sClientApplicationDirectory = Marshal.PtrToStringAnsi(pAddress);
                    }

                    Console.WriteLine("Session ID : " + oSessionInfo.iSessionID);
                    Console.WriteLine("Session State : " + oSessionInfo.oState);
                    Console.WriteLine("Workstation Name : " +
                                      oSessionInfo.sWinsWorkstationName);
                    Console.WriteLine("IP Address : " + sIPAddress);
                    Console.WriteLine("User Name : " + sDomain + @"\" + sUserName);
                    Console.WriteLine("Client Display Resolution: " +
                                      oClientDisplay.iHorizontalResolution + " x " +
                                      oClientDisplay.iVerticalResolution);
                    Console.WriteLine("Client Display Colour Depth: " +
                                      oClientDisplay.iColorDepth);
                    Console.WriteLine("Client Application Directory: " +
                                      sClientApplicationDirectory);

                    Console.WriteLine("-----------------------");
                }

                WTSFreeMemory(pSessionInfo);
            }
        }
        /// <summary>
        /// Method GetClientDisplay.
        /// </summary>
        /// <returns>Client display.</returns>
        private ClientDisplay GetClientDisplay()
        {
            WTS_CLIENT_DISPLAY clientDisplay = NativeMethodsHelper.QuerySessionInformationForStruct <WTS_CLIENT_DISPLAY>(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientDisplay);

            return(new ClientDisplay(clientDisplay));
        }
        /// <param name="pServer"></param>
        /// <param name="pSessionInfo"></param>
        private TerminalSessionInfo GetSessionInfo(IntPtr pServer, IntPtr pSessionInfo)
        {
            int  iCurrent  = (int)pSessionInfo;
            uint iReturned = 0;
            WTS_CLIENT_ADDRESS       oClientAddres = new WTS_CLIENT_ADDRESS();
            WTS_CLIENT_DISPLAY       oClientDisplay = new WTS_CLIENT_DISPLAY();
            WTS_CLIENT_PROTOCOL_TYPE oClientProtocol = WTS_CLIENT_PROTOCOL_TYPE.UNKNOWN;
            WTS_CLIENT_INFO          oClientInfo = new WTS_CLIENT_INFO();
            WTSINFO             oWtsInfo = new WTSINFO();
            string              sIPAddress = string.Empty;
            string              sUserName = string.Empty, sClientName = string.Empty;
            string              sDomain = string.Empty;
            string              sClientApplicationDirectory = string.Empty;
            TerminalSessionInfo retval  = new TerminalSessionInfo(0);
            // Get session info structure
            WTS_SESSION_INFO oSessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)iCurrent, typeof(WTS_SESSION_INFO));
            //Get the IP address of the Terminal Services User
            IntPtr pAddress = IntPtr.Zero;

            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientAddress, out pAddress, out iReturned) == true)
            {
                oClientAddres = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(pAddress, oClientAddres.GetType());
                sIPAddress    = oClientAddres.bAddress[2] + "." + oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4] + "." + oClientAddres.bAddress[5];
            }
            //Get the User Name of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSUserName, out pAddress, out iReturned) == true)
            {
                sUserName = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get the Client Name of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientName, out pAddress, out iReturned) == true)
            {
                sClientName = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get the Domain Name of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSDomainName, out pAddress, out iReturned) == true)
            {
                sDomain = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get the Display Information  of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDisplay, out pAddress, out iReturned) == true)
            {
                oClientDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(pAddress, oClientDisplay.GetType());
            }
            //Get the Application Directory of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDirectory, out pAddress, out iReturned) == true)
            {
                sClientApplicationDirectory = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get protocol type
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientProtocolType, out pAddress, out iReturned) == true)
            {
                oClientProtocol = (WTS_CLIENT_PROTOCOL_TYPE)Marshal.ReadInt16(pAddress);
            }
            //Get client info
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientInfo, out pAddress, out iReturned) == true)
            {
                oClientInfo = (WTS_CLIENT_INFO)Marshal.PtrToStructure(pAddress, oClientInfo.GetType());
                //sUserName = String.IsNullOrEmpty(sUserName) ? oClientInfo.UserName : sUserName;
            }
            //Get WTS info
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSSessionInfo, out pAddress, out iReturned) == true)
            {
                oWtsInfo = (WTSINFO)Marshal.PtrToStructure(pAddress, oWtsInfo.GetType());
            }
            // fill result
            retval.SessionInfo = oSessionInfo;
            //retval.SessionInfo.oState = oSessionInfo.oState;
            //retval.SessionInfo.sWinsWorkstationName = oSessionInfo.sWinsWorkstationName == null ? "" : oSessionInfo.sWinsWorkstationName;
            retval.UserName          = sUserName == null ? "" : sUserName;
            retval.ClientMachineName = sClientName == null ? "" : sClientName;
            retval.ClientIPAddress   = sIPAddress == null ? "" : sIPAddress;
            retval.Domain            = sDomain == null ? "" : sDomain;
            retval.ProtocolType      = oClientProtocol;
            retval.ClientInfo        = oClientInfo;
            retval.WtsInfo           = oWtsInfo;
            return(retval);
        }
Пример #10
0
        static void Main(string[] args)
        {
            IntPtr pServer   = IntPtr.Zero;
            string sUserName = string.Empty;
            string sDomain   = string.Empty;
            string sClientApplicationDirectory = string.Empty;
            string sIPAddress = string.Empty;

            WTS_CLIENT_ADDRESS oClientAddres  = new WTS_CLIENT_ADDRESS();
            WTS_CLIENT_DISPLAY oClientDisplay = new WTS_CLIENT_DISPLAY();

            IntPtr pSessionInfo = IntPtr.Zero;

            int iCount       = 0;
            int iReturnValue = WTSEnumerateSessions
                                   (pServer, 0, 1, ref pSessionInfo, ref iCount);
            int iDataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

            int iCurrent = (int)pSessionInfo;

            if (iReturnValue != 0)
            {
                //Go to all sessions
                for (int i = 0; i < iCount; i++)
                {
                    WTS_SESSION_INFO oSessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)iCurrent, typeof(WTS_SESSION_INFO));
                    iCurrent += iDataSize;

                    uint iReturned = 0;

                    //Get the IP address of the Terminal Services User
                    IntPtr pAddress = IntPtr.Zero;
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientAddress, out pAddress, out iReturned) == true)
                    {
                        oClientAddres = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(pAddress, oClientAddres.GetType());
                        sIPAddress    = oClientAddres.bAddress[2] + "." + oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4] + "." + oClientAddres.bAddress[5];
                    }
                    //Get the User Name of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSUserName, out pAddress, out iReturned) == true)
                    {
                        sUserName = Marshal.PtrToStringAnsi(pAddress);
                    }
                    //Get the Domain Name of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSDomainName, out pAddress, out iReturned) == true)
                    {
                        sDomain = Marshal.PtrToStringAnsi(pAddress);
                    }
                    //Get the Display Information  of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDisplay, out pAddress, out iReturned) == true)
                    {
                        oClientDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(pAddress, oClientDisplay.GetType());
                    }
                    //Get the Application Directory of the Terminal Services User
                    if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDirectory, out pAddress, out iReturned) == true)
                    {
                        sClientApplicationDirectory = Marshal.PtrToStringAnsi(pAddress);
                    }

                    Console.WriteLine("Session ID : " + oSessionInfo.iSessionID);
                    Console.WriteLine("Session State : " + oSessionInfo.oState);
                    Console.WriteLine("Workstation Name : " + oSessionInfo.sWinsWorkstationName);
                    Console.WriteLine("IP Address : " + sIPAddress);
                    Console.WriteLine("User Name : " + sDomain + @"\" + sUserName);
                    Console.WriteLine("Client Display Resolution: " + oClientDisplay.iHorizontalResolution + " x " + oClientDisplay.iVerticalResolution);
                    Console.WriteLine("Client Display Colour Depth: " + oClientDisplay.iColorDepth);
                    Console.WriteLine("Client Application Directory: " + sClientApplicationDirectory);

                    Console.WriteLine("-----------------------");
                }

                WTSFreeMemory(pSessionInfo);
            }

            Console.ReadKey();
        }
Пример #11
0
 public ClientDisplay(WTS_CLIENT_DISPLAY clientDisplay)
 {
     _horizontalResolution = clientDisplay.HorizontalResolution;
     _verticalResolution   = clientDisplay.VerticalResolution;
     _bitsPerPixel         = GetBitsPerPixel(clientDisplay.ColorDepth);
 }