Exemple #1
0
        /// <summary>
        /// Gets list of the sessions on the terminal server.
        /// </summary>
        /// <returns>A list of sessions.</returns>
        public List <TerminalServicesSession> GetSessions()
        {
            this.CheckDisposed();

            var results = new List <TerminalServicesSession>();

            var sessionInfos = NativeMethodsHelper.GetSessionInfos(this.Handle);

            foreach (WTS_SESSION_INFO sessionInfo in sessionInfos)
            {
                results.Add(new TerminalServicesSession(this, sessionInfo));
            }

            return(results);
        }
        /// <summary>
        /// Method LoadWinStationInformationProperties.
        /// </summary>
        private void LoadWinStationInformationProperties()
        {
            var winStationInfo = NativeMethodsHelper.GetWinStationInformation(this._server.Handle, this._sessionId);

            this._windowStationName.Value  = winStationInfo.WinStationName;
            this._connectState.Value       = winStationInfo.ConnectState;
            this._connectTime.Value        = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.ConnectTime);
            this._currentTime.Value        = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.CurrentTime);
            this._disconnectTime.Value     = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.DisconnectTime);
            this._lastInputTime.Value      = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.LastInputTime);
            this._loginTime.Value          = NativeMethodsHelper.FileTimeToDateTime(winStationInfo.LoginTime);
            this._userName.Value           = winStationInfo.UserName;
            this._domainName.Value         = winStationInfo.Domain;
            this._incomingStatistics.Value = new ProtocolStatistics(winStationInfo.ProtocolStatus.Input);
            this._outgoingStatistics.Value = new ProtocolStatistics(winStationInfo.ProtocolStatus.Output);
        }
        /// <summary>
        /// Method LoadWtsInfoProperties.
        /// </summary>
        private void LoadWtsInfoProperties()
        {
            var info = NativeMethodsHelper.QuerySessionInformationForStruct <WTSINFO>(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSSessionInfo);

            this._connectState.Value       = info.ConnectState;
            this._incomingStatistics.Value = new ProtocolStatistics(info.IncomingBytes, info.IncomingFrames, info.IncomingCompressedBytes);
            this._outgoingStatistics.Value = new ProtocolStatistics(info.OutgoingBytes, info.OutgoingFrames, info.OutgoingCompressedBytes);
            this._windowStationName.Value  = info.WinStationName;
            this._domainName.Value         = info.Domain;
            this._userName.Value           = info.UserName;
            this._connectTime.Value        = NativeMethodsHelper.FileTimeToDateTime(info.ConnectTime);
            this._disconnectTime.Value     = NativeMethodsHelper.FileTimeToDateTime(info.DisconnectTime);
            this._lastInputTime.Value      = NativeMethodsHelper.FileTimeToDateTime(info.LastInputTime);
            this._loginTime.Value          = NativeMethodsHelper.FileTimeToDateTime(info.LogonTime);
            this._currentTime.Value        = NativeMethodsHelper.FileTimeToDateTime(info.CurrentTime);
        }
        /// <summary>
        /// Connects this session to an existing session. Both sessions must be running on the local server.
        /// </summary>
        /// <param name="target">The session to which to connect.</param>
        /// <param name="password">The password of the user logged on to the target session. If the user logged on to the target session is the same as the user logged on to this session, this parameter can be an empty string.</param>
        /// <param name="synchronous">true to waits until the operation has completed before returning from the method. false to returns immediately, even though the operation may not be complete yet.</param>
        public void Connect(TerminalServicesSession target, string password, bool synchronous)
        {
            if (!this.IsLocal)
            {
                throw new InvalidOperationException("Cannot connect sessions that are running on remote servers");
            }

            if (IsVistaSp1OrHigher)
            {
                NativeMethodsHelper.Connect(this._sessionId, target.SessionId, password, synchronous);
            }
            else
            {
                NativeMethodsHelper.LegacyConnect(this._server.Handle, this._sessionId, target.SessionId, password, synchronous);
            }
        }
        /// <summary>
        /// Stops remote control of the session. The session must be running on the local server.
        /// </summary>
        public void StopRemoteControl()
        {
            if (!this.IsLocal)
            {
                throw new InvalidOperationException(
                          "Cannot stop remote control on sessions that are running on remote servers");
            }

            if (IsVistaSp1OrHigher)
            {
                NativeMethodsHelper.StopRemoteControl(this._sessionId);
            }
            else
            {
                NativeMethodsHelper.LegacyStopRemoteControl(this._server.Handle, this._sessionId, true);
            }
        }
        /// <summary>
        /// Method OnTimerElapsed
        /// </summary>
        /// <param name="obj">An object containing application-specific information relevant to the method invoked by this delegate, or null.</param>
        private void OnTimerElapsed(object obj)
        {
            if (NativeMethodsHelper.GetLastInputTime() >= this.IdleTimeout)
            {
                if (this._autoStop)
                {
                    this.Stop();
                }

                // Copy a reference to the delegate field now into a temporary field for thread safety
                EventHandler temp = Interlocked.CompareExchange(ref this.IdleOccurred, null, null);

                if (temp != null)
                {
                    temp(this, EventArgs.Empty);
                }
            }
        }
Exemple #7
0
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);

            if (PlatformYardımcısı.MonodaÇalışıyor)
            {
                return;
            }

            if (PlatformYardımcısı.VistaYadaÜstü)
            {
                NativeMethods.SetWindowTheme(this.Handle, "explorer", null);
            }

            if (PlatformYardımcısı.XpYadaÜstü)
            {
                NativeMethods.SendMessage(this.Handle, WM_CHANGEUISTATE,
                                          NativeMethodsHelper.MakeLong(UIS_SET, UISF_HIDEFOCUS), 0);
            }
        }
Exemple #8
0
        private IntPtr GetWindowToSetAsForeground()
        {
            IntPtr windowPtr = default;

            NativeMethodsHelper.EnumerateWindows(hWnd =>
            {
                // Condition order important -> the VirtualDesktop call is a (potential) RPC so if we
                // can short circuit the check it would be better
                var matchedWindow = IsValidWindow(hWnd) && _client.IsWindowOnCurrentDesktop(hWnd);
                if (!matchedWindow)
                {
                    return(true);                // Carry on search
                }
                Logger.Debug(() => $"Found valid window to focus: {DumpHWndTitle(hWnd)} - { GetProcessForHWnd(hWnd) }");

                windowPtr = hWnd;
                return(false);
            });

            return(windowPtr);
        }
Exemple #9
0
        /// <summary>
        /// Raises the <see cref="E:HandleCreated" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);

            if (PlatformHelper.RunningOnMono)
            {
                return;
            }

            if (PlatformHelper.VistaOrHigher)
            {
                // set window theme to explorer
                NativeMethods.SetWindowTheme(this.Handle, "explorer", null);
            }

            if (PlatformHelper.XpOrHigher)
            {
                // removes the ugly dotted line around focused item
                NativeMethods.SendMessage(this.Handle, WM_CHANGEUISTATE,
                                          NativeMethodsHelper.MakeLong(UIS_SET, UISF_HIDEFOCUS), 0);
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            IntPtr windowPtr = NativeMethodsHelper.GetWindowByTitle("TicTacToe");

            // Find TextBox with current player char
            IWindowControl windowControl            = new WindowControl(windowPtr, null);
            IEnumerable <IWindowControl> groupBoxes = windowControl.GetChildControls();

            PrintTexts(groupBoxes, 1);
            IWindowControl control = NativeMethodsHelper.GetWindowControlByText(windowControl, "Current Player Turn");
            string         text    = control.GetControlValue().ToString();

            Console.WriteLine(text);

            // Check current player char
            IWindowControl charControl = control.GetChildControls().FirstOrDefault();
            string         tttChar     = charControl.GetControlValue().ToString();

            Console.WriteLine(tttChar);

            Console.ReadKey();
        }
Exemple #11
0
        public static string GetCommandLineByProcessId(int processId)
        {
            string result = string.Empty;

            ManagementObjectSearcher managementObjectSearcher = null;

            try
            {
                managementObjectSearcher = new ManagementObjectSearcher(string.Format("SELECT CommandLine FROM Win32_Process WHERE ProcessId = {0}", processId));

                foreach (ManagementObject managementObject in managementObjectSearcher.Get())
                {
                    try
                    {
                        result = managementObject["CommandLine"].ToString();

                        break;
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                    }
                }
            }
            catch
            {
                result = NativeMethodsHelper.GetCommandLine(processId);
            }
            finally
            {
                if (managementObjectSearcher != null)
                {
                    managementObjectSearcher.Dispose();
                    managementObjectSearcher = null;
                }
            }

            return(result);
        }
Exemple #12
0
        public static void HandleDoMouseEvent(Packets.ServerPackets.DoMouseEvent command, Client client)
        {
            try
            {
                Screen[] allScreens = Screen.AllScreens;
                int      offsetX    = allScreens[command.MonitorIndex].Bounds.X;
                int      offsetY    = allScreens[command.MonitorIndex].Bounds.Y;
                Point    p          = new Point(command.X + offsetX, command.Y + offsetY);

                switch (command.Action)
                {
                case MouseAction.LeftDown:
                case MouseAction.LeftUp:
                    NativeMethodsHelper.DoMouseLeftClick(p, command.IsMouseDown);
                    break;

                case MouseAction.RightDown:
                case MouseAction.RightUp:
                    NativeMethodsHelper.DoMouseRightClick(p, command.IsMouseDown);
                    break;

                case MouseAction.MoveCursor:
                    NativeMethodsHelper.DoMouseMove(p);
                    break;

                case MouseAction.ScrollDown:
                    NativeMethodsHelper.DoMouseScroll(p, true);
                    break;

                case MouseAction.ScrollUp:
                    NativeMethodsHelper.DoMouseScroll(p, false);
                    break;
                }
            }
            catch
            {
            }
        }
        public static void HandleDoMouseEvent(Paketler.ServerPaketleri.DoMouseEvent command, Client client)
        {
            try
            {
                Screen[] allScreens = Screen.AllScreens;
                int      offsetX    = allScreens[command.MonitorIndex].Bounds.X;
                int      offsetY    = allScreens[command.MonitorIndex].Bounds.Y;
                Point    p          = new Point(command.X + offsetX, command.Y + offsetY);

                switch (command.Action)
                {
                case FareEylemleri.SolAşağı:
                case FareEylemleri.SolYukarı:
                    NativeMethodsHelper.DoMouseLeftClick(p, command.IsMouseDown);
                    break;

                case FareEylemleri.SağAşağı:
                case FareEylemleri.SağYukarı:
                    NativeMethodsHelper.DoMouseRightClick(p, command.IsMouseDown);
                    break;

                case FareEylemleri.ImleçOynat:
                    NativeMethodsHelper.DoMouseMove(p);
                    break;

                case FareEylemleri.AşağıTekerlek:
                    NativeMethodsHelper.DoMouseScroll(p, true);
                    break;

                case FareEylemleri.YukarıTekerlek:
                    NativeMethodsHelper.DoMouseScroll(p, false);
                    break;
                }
            }
            catch
            {
            }
        }
 /// <summary>
 /// Method GetApplicationName.
 /// </summary>
 /// <returns>Application name.</returns>
 private string GetApplicationName()
 {
     return(NativeMethodsHelper.QuerySessionInformationForString(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSApplicationName));
 }
Exemple #15
0
 public static void HandleDoKeyboardEvent(Packets.ServerPackets.DoKeyboardEvent command, Client client)
 {
     NativeMethodsHelper.DoKeyPress(command.Key, command.KeyDown);
 }
 /// <summary>
 /// Method GetClientBuildNumber.
 /// </summary>
 /// <returns>Client BuildNumber.</returns>
 private int GetClientBuildNumber()
 {
     return(NativeMethodsHelper.QuerySessionInformationForInt(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientBuildNumber));
 }
        /// <summary>
        /// Method GetClientIPAddress.
        /// </summary>
        /// <returns>Client IPAddress.</returns>
        private IPAddress GetClientIPAddress()
        {
            var clientAddress = NativeMethodsHelper.QuerySessionInformationForStruct <WTS_CLIENT_ADDRESS>(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientAddress);

            return(NativeMethodsHelper.ExtractIPAddress(clientAddress.AddressFamily, clientAddress.Address));
        }
        /// <summary>
        /// Method GetClientDisplay.
        /// </summary>
        /// <returns>Client display.</returns>
        private ClientDisplay GetClientDisplay()
        {
            var clientDisplay = NativeMethodsHelper.QuerySessionInformationForStruct <WTS_CLIENT_DISPLAY>(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientDisplay);

            return(new ClientDisplay(clientDisplay));
        }
 /// <summary>
 /// Method GetClientDirectory.
 /// </summary>
 /// <returns>Client directory.</returns>
 private string GetClientDirectory()
 {
     return(NativeMethodsHelper.QuerySessionInformationForString(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientDirectory));
 }
 /// <summary>
 /// Method GetClientProductId.
 /// </summary>
 /// <returns>Client ProductId.</returns>
 private short GetClientProductId()
 {
     return(NativeMethodsHelper.QuerySessionInformationForShort(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientProductId));
 }
Exemple #21
0
 /// <summary>
 /// Selects all items on the given listview.
 /// </summary>
 /// <param name="targetListView">The listview whose items are to be selected.</param>
 public static void SelectAllItems(this ListView targetListView)
 {
     NativeMethodsHelper.SetItemState(targetListView.Handle, -1, 2, 2);
 }
Exemple #22
0
        public static string GetActiveWindowTitle()
        {
            string title = NativeMethodsHelper.GetForegroundWindowTitle();

            return((!string.IsNullOrEmpty(title)) ? title : null);
        }
 /// <summary>
 /// Logs the session off, disconnecting any user that might be attached.
 /// </summary>
 /// <param name="synchronous">true to waits until the session is fully logged off before returning from the method. false to returns immediately, even though the session may not be completely logged off yet.</param>
 public void Logoff(bool synchronous = false)
 {
     NativeMethodsHelper.LogoffSession(this._server.Handle, this._sessionId, synchronous);
 }
 /// <summary>
 /// Disconnects any attached user from the session.
 /// </summary>
 /// <param name="synchronous">true to waits until the session is fully disconnected before returning from the method. false to returns immediately, even though the session may not be completely disconnected yet.</param>
 public void Disconnect(bool synchronous = false)
 {
     NativeMethodsHelper.DisconnectSession(this._server.Handle, this._sessionId, synchronous);
 }
Exemple #25
0
        /// <summary>
        /// Shuts down the terminal server.
        /// </summary>
        /// <param name="type">Type of shutdown requested.</param>
        public void Shutdown(ShutdownType type)
        {
            this.CheckDisposed();

            NativeMethodsHelper.ShutdownSystem(this.Handle, (int)type);
        }
 /// <summary>
 /// Method GetRemoteEndPoint.
 /// </summary>
 /// <returns>Remote EndPoint.</returns>
 private EndPoint GetRemoteEndPoint()
 {
     return(NativeMethodsHelper.QuerySessionInformationForEndPoint(this._server.Handle, this._sessionId));
 }
Exemple #27
0
 private void txtConsoleOutput_TextChanged(object sender, EventArgs e)
 {
     NativeMethodsHelper.ScrollToBottom(txtConsoleOutput.Handle);
 }
 /// <summary>
 /// Method GetInitialProgram.
 /// </summary>
 /// <returns>Initial program name.</returns>
 private string GetInitialProgram()
 {
     return(NativeMethodsHelper.QuerySessionInformationForString(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSInitialProgram));
 }
 public void LockWorkstation()
 {
     NativeMethodsHelper.LockWorkStation();
 }
 /// <summary>
 /// Method GetClientProtocolType.
 /// </summary>
 /// <returns>Client ProtocolType.</returns>
 private ClientProtocolType GetClientProtocolType()
 {
     return((ClientProtocolType)NativeMethodsHelper.QuerySessionInformationForShort(this._server.Handle, this._sessionId, WTS_INFO_CLASS.WTSClientProtocolType));
 }