예제 #1
0
 public SessionChangeHandler(IPInvokeHandler pinvokeHandler)
 {
     this.pinvokeHandler = pinvokeHandler;
     _registered         = true;
     if (!pinvokeHandler.WTSRegisterSessionNotification(this.Handle, PInvoker.NOTIFY_FOR_THIS_SESSION))
     {
         _registered = false;
         Log.Warning($"Problem registering session notification. Problem might arise with lock screen. error: {pinvokeHandler.GetLastError()}");
     }
 }
예제 #2
0
        /// <summary>
        /// Validates if an specific hWnd could probably be handled by WindowNode
        /// </summary>
        /// <param name="hWnd">window handler to validate</param>
        /// <param name="validation">Instruction on what to validate</param>
        /// <returns>true if it probably can</returns>
        public bool CanHandleHwnd(IntPtr hWnd, ValidateHwndParams validation)
        {
            if (validation.DoValidate == false)
            {
                return(true);
            }

            if (hWnd == IntPtr.Zero)
            {
                return(false);
            }

            if (ShouldIgnoreHwnd(hWnd))
            {
                return(false);
            }

            var cb      = new StringBuilder(1024);
            var style   = pinvokeHandler.GetWindowLongPtr(hWnd, PInvoker.GWL_STYLE).ToInt64();
            var exstyle = pinvokeHandler.GetWindowLongPtr(hWnd, PInvoker.GWL_EXSTYLE).ToInt64();

            if (style == 0)
            {
                Log.Warning($"{nameof(WindowNode)}.{nameof(CanHandleHwnd)} Could not retrieve GWL_STYLE for hWnd {hWnd}, error: {pinvokeHandler.GetLastError()}");
                return(false);
            }

            if (validation.ValidateChild && (style & PInvoker.WS_CHILD) == PInvoker.WS_CHILD)
            {
                return(false);
            }

            if (validation.ValidateVisible && (style & PInvoker.WS_VISIBLE) != PInvoker.WS_VISIBLE)
            {
                return(false);
            }

            if (validation.ValidateNoActivate && (exstyle & PInvoker.WS_EX_NOACTIVATE) == PInvoker.WS_EX_NOACTIVATE)
            {
                return(false);
            }

            if (pinvokeHandler.GetClassName(hWnd, cb, cb.Capacity) == 0)
            {
                Log.Warning($"{nameof(WindowNode)}.{nameof(CanHandleHwnd)} Could not retrieve class name for hWnd {hWnd}, error: {pinvokeHandler.GetLastError()}");
                return(false);
            }

            var className = cb.ToString();

            if (classNamesToIgnore.Any(regex => regex.IsMatch(className)))
            {
                return(false);
            }

            if (validation.ValidateApplicationFrame && className == "ApplicationFrameWindow")
            {
                if (IsSpecialAppVisible(hWnd) == false)
                {
                    Log.Warning($"{nameof(WindowNode)}.{nameof(CanHandleHwnd)} Going to ignore {hWnd} \"{GetWindowText(hWnd)}\" [{className}] because it is not visible according to window 10 special thingy");
                    return(false);
                }
            }

            if (validation.ValidateDwm && className == "Windows.UI.Core.CoreWindow")
            {
                var result = 0;
                if ((result = pinvokeHandler.DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.Cloaked, out bool pvAttribute, sizeof(int))) != 0 || pvAttribute)
                {
                    return(false);
                }
            }

            var text = GetWindowText(hWnd);

            if (captionsToIgnore.Any(regex => regex.IsMatch(text)))
            {
                return(false);
            }

            return(true);
        }