Example #1
0
        public static TerminalServer GetSessions(string ServerName)
        {
            TerminalServer Data = new TerminalServer();

            Data.ServerName = ServerName;


            IntPtr ptrOpenedServer = IntPtr.Zero;

            try
            {
                ptrOpenedServer = WTSOpenServer(ServerName);
                if (ptrOpenedServer == System.IntPtr.Zero)
                {
                    Data.IsATerminalServer = false;
                    return(Data);
                }
                Data.ServerPointer     = ptrOpenedServer;
                Data.IsATerminalServer = true;

                Int32  FRetVal;
                IntPtr ppSessionInfo = IntPtr.Zero;
                Int32  Count         = 0;
                try
                {
                    FRetVal = WTSEnumerateSessions(ptrOpenedServer, 0, 1, ref ppSessionInfo, ref Count);

                    if (FRetVal != 0)
                    {
                        Data.Sessions = new List <Session>();
                        WTS_SESSION_INFO[] sessionInfo = new WTS_SESSION_INFO[Count + 1];
                        int           i;
                        System.IntPtr session_ptr;
                        for (i = 0; i <= Count - 1; i++)
                        {
                            session_ptr    = new System.IntPtr(ppSessionInfo.ToInt32() + (i * Marshal.SizeOf(sessionInfo[i])));
                            sessionInfo[i] = (WTS_SESSION_INFO)Marshal.PtrToStructure(session_ptr, typeof(WTS_SESSION_INFO));
                            Session s = new Session();
                            s.SessionID          = sessionInfo[i].SessionID;
                            s.State              = (ConnectionStates)(int)sessionInfo[i].State;
                            s.WindowsStationName = sessionInfo[i].pWinStationName;
                            s.ServerName         = ServerName;
                            Data.Sessions.Add(s);
                        }
                        WTSFreeMemory(ppSessionInfo);
                        strSessionsInfo[] tmpArr = new strSessionsInfo[sessionInfo.GetUpperBound(0) + 1];
                        for (i = 0; i <= tmpArr.GetUpperBound(0); i++)
                        {
                            tmpArr[i].SessionID       = sessionInfo[i].SessionID;
                            tmpArr[i].StationName     = sessionInfo[i].pWinStationName;
                            tmpArr[i].ConnectionState = GetConnectionState(sessionInfo[i].State);
                            //MessageBox.Show(tmpArr(i).StationName & " " & tmpArr(i).SessionID & " " & tmpArr(i).ConnectionState)
                        }
                        // ERROR: Not supported in C#: ReDimStatement
                    }
                }
                catch (Exception ex)
                {
                    Logging.Error("Get Sessions Inner", ex);
                    Data.Errors.Add(ex.Message + "\r\n" + System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }
            }
            catch (Exception ex)
            {
                Logging.Info("Get Sessions Outer", ex);
                Data.Errors.Add(ex.Message + "\r\n" + System.Runtime.InteropServices.Marshal.GetLastWin32Error());
            }

            WTS_PROCESS_INFO[] plist = WTSEnumerateProcesses(ptrOpenedServer, Data);

            //Get ProcessID of TS Session that executed this TS Session
            Int32 active_process = GetCurrentProcessId();
            Int32 active_session = 0;
            bool  success1       = ProcessIdToSessionId(active_process, ref active_session);

            if (active_session <= 0)
            {
                success1 = false;
            }
            if (Data != null && Data.Sessions != null)
            {
                foreach (Session s in Data.Sessions)
                {
                    if (s.Client == null)
                    {
                        s.Client = new Client();
                    }

                    WTS_CLIENT_INFO ClientInfo = LoadClientInfoForSession(Data.ServerPointer, s.SessionID);
                    s.Client.Address       = ClientInfo.Address;
                    s.Client.AddressFamily = ClientInfo.AddressFamily;
                    s.Client.ClientName    = ClientInfo.WTSClientName;
                    s.Client.DomianName    = ClientInfo.WTSDomainName;
                    s.Client.StationName   = ClientInfo.WTSStationName;
                    s.Client.Status        = ClientInfo.WTSStatus;
                    s.Client.UserName      = ClientInfo.WTSUserName;
                    s.IsTheActiveSession   = false;
                    if (success1 && s.SessionID == active_session)
                    {
                        s.IsTheActiveSession = true;
                    }
                }
            }

            WTSCloseServer(ptrOpenedServer);
            return(Data);
        }
        public static TerminalServer GetSessions(string ServerName)
        {
            TerminalServer Data = new TerminalServer {ServerName = ServerName};

            IntPtr ptrOpenedServer = IntPtr.Zero;
            try
            {
                ptrOpenedServer = WTSOpenServer(ServerName);

                if (ptrOpenedServer == IntPtr.Zero)
                {
                    Data.IsATerminalServer = false;
                    return Data;
                }

                Data.ServerPointer = ptrOpenedServer;
                Data.IsATerminalServer = true;

                IntPtr ppSessionInfo = IntPtr.Zero;
                Int32 Count = 0;

                try
                {
                    Int32 FRetVal = WTSEnumerateSessions(ptrOpenedServer, 0, 1, ref ppSessionInfo, ref Count);

                    if (FRetVal != 0)
                    {
                        Data.Sessions = new List<Session>();
                        WTS_SESSION_INFO[] sessionInfo = new WTS_SESSION_INFO[Count + 1];
                        int i;

                        for (i = 0; i <= Count - 1; i++)
                        {
                            IntPtr session_ptr = new IntPtr(ppSessionInfo.ToInt32() + (i*Marshal.SizeOf(sessionInfo[i])));
                            sessionInfo[i] =
                                (WTS_SESSION_INFO) Marshal.PtrToStructure(session_ptr, typeof (WTS_SESSION_INFO));
                            Data.Sessions.Add(new Session
                                            {
                                                SessionId = sessionInfo[i].SessionID,
                                                State = (ConnectionStates) (int) sessionInfo[i].State,
                                                WindowsStationName = sessionInfo[i].pWinStationName,
                                                ServerName = ServerName
                                            });
                        }

                        WTSFreeMemory(ppSessionInfo);
                        strSessionsInfo[] tmpArr = new strSessionsInfo[sessionInfo.GetUpperBound(0) + 1];

                        for (i = 0; i <= tmpArr.GetUpperBound(0); i++)
                        {
                            tmpArr[i].SessionID = sessionInfo[i].SessionID;
                            tmpArr[i].StationName = sessionInfo[i].pWinStationName;
                            tmpArr[i].ConnectionState = GetConnectionState(sessionInfo[i].State);
                        }
                        // ERROR: Not supported in C#: ReDimStatement 
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Get Sessions Inner", ex);
                    Data.Errors.Add(ex.Message + "\r\n" + Marshal.GetLastWin32Error());
                }
            }
            catch (Exception ex)
            {
                Log.Info("Get Sessions Outer", ex);
                Data.Errors.Add(ex.Message + "\r\n" + Marshal.GetLastWin32Error());
            }

            WTS_PROCESS_INFO[] plist = WTSEnumerateProcesses(ptrOpenedServer, Data);

            //Get ProcessID of TS Session that executed this TS Session 
            Int32 active_process = GetCurrentProcessId();
            Int32 active_session = 0;

            bool success1 = ProcessIdToSessionId(active_process, ref active_session);

            if (active_session <= 0) success1 = false;

            if (Data != null && Data.Sessions != null)
            {
                foreach (Session s in Data.Sessions)
                {
                    if (s.Client == null) s.Client = new Client();

                    WTS_CLIENT_INFO ClientInfo = LoadClientInfoForSession(Data.ServerPointer, s.SessionId);
                    s.Client.Address = ClientInfo.Address;
                    s.Client.AddressFamily = ClientInfo.AddressFamily;
                    s.Client.ClientName = ClientInfo.WTSClientName;
                    s.Client.DomianName = ClientInfo.WTSDomainName;
                    s.Client.StationName = ClientInfo.WTSStationName;
                    s.Client.Status = ClientInfo.WTSStatus;
                    s.Client.UserName = ClientInfo.WTSUserName;
                    s.IsTheActiveSession = false;

                    if (success1 && s.SessionId == active_session) s.IsTheActiveSession = true;
                }
            }

            WTSCloseServer(ptrOpenedServer);
            return Data;
        }