Example #1
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);
        }
        /// <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);
        }
Example #3
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);
            }
        }
Example #4
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();
        }