コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// GetRemoteClientIP
        /// </summary>
        /// <returns></returns>
        public static string GetRemoteClientIP()
        {
            WTS_CLIENT_ADDRESS oClientAddres = new WTS_CLIENT_ADDRESS();
            string             sIPAddress    = string.Empty;
            uint iReturned = 0;

            //Get the IP address of the Terminal Services User
            IntPtr pAddress = IntPtr.Zero;

            if (WTSQuerySessionInformation(IntPtr.Zero, -1, WTS_INFO_CLASS.WTSClientAddress, out pAddress, out iReturned) == true)
            {
                oClientAddres = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(pAddress, oClientAddres.GetType());
                sIPAddress    = oClientAddres.Address[2] + "." + oClientAddres.Address[3] + "." + oClientAddres.Address[4] + "." + oClientAddres.Address[5];
            }
            return(sIPAddress);
        }
コード例 #3
0
        public static List <RemoteLogonSession> GetWindowsUsers(IntPtr server)
        {
            var windowsUsers = new List <RemoteLogonSession>();

            IntPtr buffer = IntPtr.Zero;
            int    count  = 0;

            try
            {
                int   retval   = WTSEnumerateSessions(server, 0, 1, ref buffer, ref count);
                int   dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
                Int64 current  = (int)buffer;

                if (retval != 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        var  windowsUser   = new RemoteLogonSession();
                        var  bufferTwo     = IntPtr.Zero;
                        uint bytesReturned = 0;

                        WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)current, typeof(WTS_SESSION_INFO));
                        current += dataSize;
                        windowsUser.SessionId = Convert.ToUInt32(si.SessionID);

                        try
                        {
                            // Get the username of the Terminal Services user.
                            if (WTSQuerySessionInformation(server, si.SessionID, WTS_INFO_CLASS.WTSUserName, out buffer, out bytesReturned) == true)
                            {
                                windowsUser.Username = Marshal.PtrToStringAnsi(buffer).Trim();
                            }
                            if (string.IsNullOrWhiteSpace(windowsUser.Username))
                            {
                                continue;
                            }

                            // Get the connection state of the Terminal Services user.
                            if (si.State == WTS_CONNECTSTATE_CLASS.WTSDisconnected)
                            {
                                windowsUser.IsDisconnected = true;
                            }

                            // Get the IP address of the Terminal Services user.
                            if (WTSQuerySessionInformation(server, si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out buffer, out bytesReturned) == true)
                            {
                                var clientAddress = new WTS_CLIENT_ADDRESS();
                                clientAddress         = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(buffer, clientAddress.GetType());
                                windowsUser.IpAddress = clientAddress.Address[2] + "." + clientAddress.Address[3] + "." + clientAddress.Address[4] + "." + clientAddress.Address[5];
                            }

                            windowsUsers.Add(windowsUser);
                        }
                        finally
                        {
                            WTSFreeMemory(bufferTwo);
                        }
                    }
                }
            }
            finally
            {
                WTSFreeMemory(buffer);
            }


            return(windowsUsers);
        }
コード例 #4
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);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: hkuntal/TeamWorkManagement
        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);
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: hkuntal/TeamWorkManagement
        private static string GetCurrentSession()
        {
            Console.WriteLine("------------------FROM WIN32 API--------------------");

            const int WTS_CURRENT_SERVER_HANDLE = -1;
            IntPtr currentSession = IntPtr.Zero;
            IntPtr currentServer = IntPtr.Zero;
            string sUserName = string.Empty;
            string sDomain = string.Empty;
            string sessionType = string.Empty;

            IntPtr pAddress = IntPtr.Zero;
            uint iReturned = 0;
            WTS_CLIENT_ADDRESS oClientAddres = new WTS_CLIENT_ADDRESS();
            string sIPAddress = string.Empty;

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

            if (WTSQuerySessionInformation(currentServer, WTS_CURRENT_SERVER_HANDLE, 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];

                Console.WriteLine("IpAddress: " + sIPAddress);
            }
            //Get the User Name of the Terminal Services User
            if (WTSQuerySessionInformation(currentServer,
                                           WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSUserName,
                                           out pAddress, out iReturned) == true)
            {
                sUserName = Marshal.PtrToStringAnsi(pAddress);
                Console.WriteLine("User name: " + sUserName);
            }

            //Get the Domain Name of the Terminal Services User
            if (WTSQuerySessionInformation(currentServer,
                                           WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSDomainName,
                                           out pAddress, out iReturned) == true)
            {
                sDomain = Marshal.PtrToStringAnsi(pAddress);
                Console.WriteLine("Domain Name: "+sDomain);
            }

            //Get the session Name of the Terminal Services User
            if (WTSQuerySessionInformation(currentServer,
                                           WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSWinStationName,
                                           out pAddress, out iReturned) == true)
            {
                sessionType = Marshal.PtrToStringAnsi(pAddress);
                Console.WriteLine("Session Type: " + sessionType);
            }

            //Get the session Name of the Terminal Services User
            if (WTSQuerySessionInformation(currentServer,
                                           WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSSessionId,
                                           out pAddress, out iReturned) == true)
            {
                var sessionId = Marshal.ReadIntPtr(pAddress);
                Console.WriteLine("Session Id: " + sessionId);
            }

            Console.WriteLine("------------------FROM WIN32 API--------------------");
            return sessionType;
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: cupid0426/MyProject
        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();
        }
コード例 #9
0
        private static string GetWTSInformation(WTS_INFO_CLASS whatInfoNeeded)
        {
            IntPtr buffer = IntPtr.Zero;
            Int32  bytesReturned;

            string strClientName = "";

            try
            {
                WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
                bool sucess = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, whatInfoNeeded, out buffer, out bytesReturned);
                if (sucess)
                {
                    if (whatInfoNeeded == WTS_INFO_CLASS.WTSClientAddress)
                    {
                        WTS_CLIENT_ADDRESS oClientAddres = new WTS_CLIENT_ADDRESS();
                        try
                        {
                            oClientAddres = (WTS_CLIENT_ADDRESS)System.Runtime.InteropServices.Marshal.PtrToStructure(buffer, oClientAddres.GetType());
                        }
                        catch
                        {
                        }
                        strClientName = oClientAddres.bAddress[2] + "." + oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4] + "." + oClientAddres.bAddress[5];
                    }
                    else
                    {
                        strClientName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(buffer);
                    }
                }
            }
            catch
            {
            }

            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    try
                    {
                        WTSFreeMemory(buffer);
                    }
                    catch
                    {
                    }
                    buffer = IntPtr.Zero;
                }
            }
            return(strClientName);
        }