public void Connect(Kohl.Framework.Security.Credential credentials)
        {
            progress.Visible = true;
            splitContainer1.Visible = false;

            this.SelectedSession = null;
            this.dataGridView1.DataSource = null;
            this.dataGridView2.DataSource = null;
            this.propertyGrid1.SelectedObject = null;

            this.server = TerminalServer.LoadServer(this.ServerNameComboBox.Text, credentials);

            progress.Visible = false;
            splitContainer1.Visible = true;

            if (this.server.IsTerminalServer)
            {
                this.dataGridView1.DataSource = this.server.Sessions;

                if (this.dataGridView1.Columns.Count > 0)
                    this.dataGridView1.Columns[1].Visible = false;

                if (this.server.Sessions == null)
                    MessageBox.Show("Terminals was unable to enumerate your server's sessions." + (this.server.Errors != null & this.server.Errors.Count > 0 ? "\n" +this.server.Errors[0] : "" ));
            }
            else
            {
                MessageBox.Show("This machine does not appear to be a terminal server.");
            }
        }
 private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
 {
     if(server.IsATerminalServer)
     {
         if(dataGridView1.DataSource != null)
         {
             SelectedSession = server.Sessions[e.RowIndex];
             this.propertyGrid1.SelectedObject = SelectedSession.Client;
             this.dataGridView2.DataSource = SelectedSession.Processes;
         }
     }
 }
 internal static void SendMessageToSession(Session session)
 {
     if (session != null)
     {
         string prompt = Program.Resources.GetString("Pleaseenterthemessagetosend");
         InputBoxResult result = InputBox.Show(prompt, "Send network message");
         if (result.ReturnCode == DialogResult.OK && !string.IsNullOrEmpty(result.Text))
         {
             string meessageText = result.Text.Trim();
             string messageHeader = Program.Resources.GetString("MessagefromyourAdministrator");
             TerminalServicesAPI.SendMessage(session, messageHeader, meessageText, 0, 10, false);
         }
     }
 }
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // This prevents SharpDevelop and Visual Studio from both an exception in design mode for controls using this HistoryTreeView and from crashing when opening the
            // designer for this class.
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
                return;

            if (this.server.IsATerminalServer)
            {
                if (this.dataGridView1.DataSource != null)
                {
                    this.SelectedSession = this.server.Sessions[e.RowIndex];
                    this.propertyGrid1.SelectedObject = this.SelectedSession.Client;
                    this.dataGridView2.DataSource = this.SelectedSession.Processes;
                }
            }
        }
 private void button1_Click(object sender, EventArgs e)
 {
     SelectedSession = null;
     dataGridView1.DataSource = null;
     dataGridView2.DataSource = null;
     this.propertyGrid1.SelectedObject = null;
     Application.DoEvents();
     server = TerminalServices.TerminalServer.LoadServer(this.ServerNameComboBox.Text);
     if(server.IsATerminalServer)
     {
         dataGridView1.DataSource = server.Sessions;
         dataGridView1.Columns[1].Visible = false;
     }
     else
     {
         System.Windows.Forms.MessageBox.Show("This machine does not appear to be a Terminal Server");
     }
 }
        private void ConnectButton_Click(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
                this.ParentForm.Cursor = Cursors.WaitCursor;

            this.selectedSession = null;
            dataGridView1.DataSource = null;
            dataGridView2.DataSource = null;
            this.propertyGrid1.SelectedObject = null;
            Application.DoEvents();
            server = TerminalServer.LoadServer(this.ServerNameComboBox.Text);

            try
            {
                 if (server.IsATerminalServer)
                {
                    dataGridView1.DataSource = server.Sessions;
                    dataGridView1.Columns[1].Visible = false;
                }
                else
                {
                    MessageBox.Show("This machine does not appear to be a Terminal Server");
                }
            }
            catch (Exception)
            {
                // Do nothing when error
            }

            if (this.ParentForm != null)
                this.ParentForm.Cursor = Cursors.Default;
        }
        public static bool LogOffSession(Session Session, bool Wait)
        {
            IntPtr server = WTSOpenServer(Session.ServerName);

            if (server != IntPtr.Zero)
            {
                return WTSLogoffSession(server, Session.SessionId, Wait);
            }
            return false;
        }
        public static bool SendMessage(Session Session, string Title, string Message, int Style, int Timeout, bool Wait)
        {
            IntPtr server = WTSOpenServer(Session.ServerName);

            if (server != IntPtr.Zero)
            {
                int respose = 0;
                return WTSSendMessage(server, Session.SessionId, Title, Title.Length, Message, Message.Length, Style,
                                      Timeout, out respose, Wait);
            }
            return false;
        }
        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)
                {
                    Terminals.Logging.Log.Info("", ex);
                    Data.Errors.Add(ex.Message + "\r\n" + System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }
            }
            catch(Exception ex)
            {
                Terminals.Logging.Log.Info("", 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;
            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 bool LogOffSession(Session Session, bool Wait)
 {
     System.IntPtr server = WTSOpenServer(Session.ServerName);
     if(server != System.IntPtr.Zero)
     {
         return TerminalServicesAPI.WTSLogoffSession(server, Session.SessionID, Wait);
     }
     return false;
 }
        private static void GetClientInfos(TerminalServer terminalServer)
        {
            try
            {
                IntPtr ppSessionInfo = IntPtr.Zero;
                Int32 Count = 0;

                Int32 FRetVal = WTSEnumerateSessions(terminalServer.ServerPointer, 0, 1, ref ppSessionInfo, ref Count);

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

                    for (int 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));

                        Session session = new Session
                                        {
                                            SessionId = sessionInfo[i].SessionID,
                                            State = (ConnectionStates) (int) sessionInfo[i].State,
                                            WindowsStationName = string.IsNullOrWhiteSpace(sessionInfo[i].pWinStationName) ? "RPD-Tcp#?" : sessionInfo[i].pWinStationName,
                                            ServerName = terminalServer.ServerName
                                        };

                        session.Client = GetClientInfoForSession(terminalServer.ServerPointer, session.SessionId);
                        session.Client.Status = Enum.GetName(typeof(ConnectionStates), session.State).Replace("WTS", "");

                        terminalServer.Sessions.Add(session);
                    }

                    WTSFreeMemory(ppSessionInfo);
                }
            }
            catch (Exception ex)
            {
                Log.Info("Error enumerating RDP sessions.", ex);
                terminalServer.Errors.Add(ex.Message + "\r\n" + Marshal.GetLastWin32Error());
            }
        }