0

Is there any way to recognize if login screen is displayed ?

I trued to use OpenInputDesktop function but it not reliable (sometime it works but sometimes not)

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr OpenInputDesktop(uint dwFlags, bool fInherit, uint dwDesiredAccess);

var handle = OpenInputDesktop(0, false, 0);
var locked = handle == null || handle == 0;

My exact case is: Windows 10 starts all previously ran programs with user session after OS reboot. I dont want my program to do anything until Windows login screen is displayed / user unlock his session

Roxana Sh
  • 294
  • 1
  • 3
  • 14
Maciej
  • 10,423
  • 17
  • 64
  • 97
  • You can listen for [lock and unlock events](https://stackoverflow.com/a/12293351/219933) and act on it – Mat J Dec 01 '19 at 11:52
  • This will only if my app will be run after real user login (user login to OS then locks session). My issue is app starts when session is already locked – – Maciej Dec 02 '19 at 07:15

1 Answers1

0

Try

using Microsoft.Win32;

SystemEvents.SessionSwitch += (s, e) =>
        {
            if (e.Reason == SessionSwitchReason.SessionLock)
            {
                // Do what you want here as the system is locked
            }
            else
            {
                Console.WriteLine("Unlocked by: {0}", Environment.UserName);
            }
        };
M.Fakhoury
  • 11
  • 2
  • This will only if my app will be run after real user login (user login to OS then locks session). My issue is app starts when session is already locked – Maciej Dec 02 '19 at 07:15