0

Im try to create console app in c#, that will run all day and will fight against Enforcing lock screen after idle time via GPO. Basicaly its working but I need somehow catch event in the app if screen lock is actived or unactived. Here is the code https://github.com/devuserPP/AutoClicker.

I ques could be possible over this https://learn.microsoft.com/en-us/windows/win32/termserv/wm-wtssession-change?redirectedfrom=MSDN. Problem is Im beginer in c# and do not have idea how implement this.

Can someone help me or advice what is the best way to solve this task.

Thank you in advance.

  • [WTSRegisterSessionNotification](https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsregistersessionnotification) -- You get `WTS_SESSION_LOCK` in WndProc when the lock screen is about to be presented. – Jimi Jul 12 '22 at 09:32
  • Forgot to mention: you should register a Window handle with `NOTIFY_FOR_THIS_SESSION = 0`. When you get a `WM_WTSSESSION_CHANGE = 0x02B1` message, the session status change is in WParam (`WTS_SESSION_LOCK = 0x7`) – Jimi Jul 12 '22 at 09:55
  • can you please make for me short code example for better understanding. – user19532719 Jul 12 '22 at 10:13
  • Since you have a Console app, you can create a [NativeWindow](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.nativewindow) (importing `System.Windows.Forms`) and override its WndProc to receive Windows messages. Use its handle in the `WTSRegisterSessionNotification()` call -- Note that you must call `WTSUnRegisterSessionNotification()` when you app closes, so you probably better use [SetConsoleCtrlHandler](https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler) and subscribe to `AppDomain.CurrentDomain.ProcessExit` to handle different close reasons. – Jimi Jul 12 '22 at 10:48
  • For an example on how to use `SetConsoleCtrlHandler`, see [Adding MenuItems to Contextmenu for a TrayIcon in a Console app](https://stackoverflow.com/a/65048753/7444103) – Jimi Jul 12 '22 at 11:02

0 Answers0