コード例 #1
0
        /// <summary>
        /// 获取TS用户回话列表
        /// </summary>
        /// <returns></returns>
        public static List <LogonUser> GetLogonUserList()
        {
            List <LogonUser> LogonUsers = null;

            #region 查询代码
            WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();
            LogonUser          cum          = null;
            LogonUsers = new System.Collections.Generic.List <LogonUser>();
            for (int i = 0; i < pSessionInfo.Length; i++)
            {
                if ("RDP-Tcp" != pSessionInfo[i].pWinStationName)
                {
                    try
                    {
                        int           count         = 0;
                        IntPtr        buffer        = IntPtr.Zero;
                        StringBuilder userName      = new StringBuilder();
                        StringBuilder clientUser    = new StringBuilder();
                        StringBuilder stateType     = new StringBuilder();
                        byte[]        protocalType  = new byte[2];
                        byte[]        connState     = new byte[1];
                        StringBuilder clientAddress = new StringBuilder();

                        bool userNameBool      = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSUserName, out userName, out count);
                        bool clientUserBool    = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientName, out clientUser, out count);
                        bool stateTypeBool     = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSWinStationName, out stateType, out count);
                        bool protocalTypeBool  = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientProtocolType, out protocalType, out count);
                        bool connStateBool     = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSConnectState, out connState, out count);
                        bool clientAddressBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientAddress, out clientAddress, out count);

                        if (userNameBool && clientUserBool && stateTypeBool & connStateBool)
                        {
                            cum                = new LogonUser();
                            cum.SessionId      = pSessionInfo[i].SessionID;
                            cum.UserName       = userName.ToString();
                            cum.ClientUserName = clientUser.ToString();
                            cum.SessionType    = stateType.ToString();
                            cum.ProtocalType   = (Silmoon.Windows.Systems.LogonUser.ClientProtocalType)((int)protocalType[0]);
                            cum.ConnectState   = (WTS_CONNECTSTATE_CLASS)connState[0];

                            WTS_CLIENT_ADDRESS ad = new WTS_CLIENT_ADDRESS();


                            //var aa = clientAddress[1];
                        }
                        LogonUsers.Add(cum);
                    }
                    catch
                    {
                    }
                }
            }
            #endregion
            return(LogonUsers);
        }
コード例 #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);
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
0
        public static RemoteSessionInfo GetRemoteSessionInfo()
        {
            RemoteSessionInfo rsi = new RemoteSessionInfo();

            try
            {
                int           sessionId = Process.GetCurrentProcess().SessionId;
                StringBuilder sb;
                IntPtr        ptr;
                int           len;

                if (wtsapi32.WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSUserName, out sb, out len))
                {
                    rsi.UserName = sb.ToString();
                }

                if (wtsapi32.WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSDomainName, out sb, out len))
                {
                    rsi.Domain = sb.ToString();
                }

                if (wtsapi32.WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSClientName, out sb, out len))
                {
                    rsi.ClientName = sb.ToString();
                }

                if (wtsapi32.WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSClientAddress, out ptr, out len))
                {
                    WTS_CLIENT_ADDRESS addr = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(ptr, typeof(WTS_CLIENT_ADDRESS));
                    if (addr.AddressFamily == util.AF_INET)
                    {
                        rsi.ClientAddress = string.Format("{0}.{1}.{2}.{3}", new object[] { addr.Address[2], addr.Address[3], addr.Address[4], addr.Address[5] });
                    }
                }

                //if (wtsapi32.WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSClientDisplay, out ptr, out len))
                //{
                //    WTS_CLIENT_DISPLAY disp = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(ptr, typeof(WTS_CLIENT_DISPLAY));
                //    rsi.ClientHResolution = disp.HorizontalResolution;
                //    rsi.ClientVResolution = disp.VerticalResolution;
                //    rsi.ClientColorDepth = disp.ColorDepth;
                //}
            }
            catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
            return(rsi);
        }
コード例 #6
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);
        }
コード例 #7
0
ファイル: TerminalTools.cs プロジェクト: sanglyb/logon-logoff
        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);
        }
コード例 #8
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);
        }
コード例 #9
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);
            }
        }
コード例 #10
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;
        }
        public static List <Dictionary <string, string> > GetRDPSessions()
        {
            List <Dictionary <string, string> > results = new List <Dictionary <string, string> >();
            // adapted from http://www.pinvoke.net/default.aspx/wtsapi32.wtsenumeratesessions
            IntPtr        server = IntPtr.Zero;
            List <String> ret    = new List <string>();

            server = OpenServer("localhost");

            try
            {
                IntPtr ppSessionInfo = IntPtr.Zero;

                Int32 count    = 0;
                Int32 level    = 1;
                Int32 retval   = WTSEnumerateSessionsEx(server, ref level, 0, ref ppSessionInfo, ref count);
                Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO_1));
                Int64 current  = (Int64)ppSessionInfo;

                if (retval != 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        Dictionary <string, string> rdp_session = new Dictionary <string, string>();
                        WTS_SESSION_INFO_1          si          = (WTS_SESSION_INFO_1)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO_1));
                        current += dataSize;
                        if (si.pUserName == null || si.pUserName == "")
                        {
                            continue;
                        }

                        rdp_session["SessionID"]    = String.Format("{0}", si.SessionID);
                        rdp_session["pSessionName"] = String.Format("{0}", si.pSessionName);
                        rdp_session["pUserName"]    = String.Format("{0}", si.pUserName);
                        rdp_session["pDomainName"]  = String.Format("{0}", si.pDomainName);
                        rdp_session["State"]        = String.Format("{0}", si.State);
                        rdp_session["SourceIP"]     = "";

                        // Now use WTSQuerySessionInformation to get the remote IP (if any) for the connection
                        IntPtr addressPtr = IntPtr.Zero;
                        uint   bytes      = 0;

                        WTSQuerySessionInformation(server, (uint)si.SessionID, WTS_INFO_CLASS.WTSClientAddress, out addressPtr, out bytes);
                        WTS_CLIENT_ADDRESS address = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure((System.IntPtr)addressPtr, typeof(WTS_CLIENT_ADDRESS));

                        if (address.Address[2] != 0)
                        {
                            string sourceIP = String.Format("{0}.{1}.{2}.{3}", address.Address[2], address.Address[3], address.Address[4], address.Address[5]);
                            rdp_session["SourceIP"] = String.Format("{0}", sourceIP);
                        }
                        results.Add(rdp_session);
                    }
                    WTSFreeMemory(ppSessionInfo);
                }
            }
            catch (Exception ex)
            {
                Beaprint.GrayPrint(String.Format("  [X] Exception: {0}", ex));
            }
            finally
            {
                CloseServer(server);
            }
            return(results);
        }
コード例 #12
0
        /// <summary>
        /// Method GetClientIPAddress.
        /// </summary>
        /// <returns>Client IPAddress.</returns>
        private IPAddress GetClientIPAddress()
        {
            WTS_CLIENT_ADDRESS clientAddress = NativeMethodsHelper.QuerySessionInformationForStruct <WTS_CLIENT_ADDRESS>(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientAddress);

            return(NativeMethodsHelper.ExtractIPAddress(clientAddress.AddressFamily, clientAddress.Address));
        }
コード例 #13
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);
        }
コード例 #14
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();
        }