0

I have a simple screensaver that we use within our company, and I'm trying to have the screensaver detect that it's running in a remote desktop session (or Terminal Services Session), and in that situation prevent the screensaver from both starting and locking the workstation.

Detecting if the session is remote is easy enough, so it's easy to exit the screensaver, but the workstation still locks.

I've tried exit codes of 0 & 1, hoping that would make a difference, but it doesn't. I've also tried using SendKeys to attempt to emulate a keypress to the screensaver's main form before the (default) 5 second grace period expires, but that doesn't work either.

Here's an example of what I've been using in the main form's constructor:

if (IsRemoteSession())
{
    SendKeys.Send(Environment.NewLine);
    Environment.Exit(0);
}

I have tested that the above condition is met.

Can anyone suggest a way in which this can be achieved?

Bryan
  • 3,224
  • 9
  • 41
  • 58
  • 1
    https://stackoverflow.com/questions/2284601/disabling-screen-saver-and-power-options-in-c-sharp – sellotape Mar 20 '20 at 18:55
  • Thanks @sellotape, I'm currently playing with some of those flags, but I'll give those specific ones in that linked question a try. – Bryan Mar 20 '20 at 19:04
  • Unfortunately @sellotape the above doesn't work, I think it's because I'm making that SetThreadExecutionState call after the screensaver has already been triggered. I suspect I'll need to make a standalone executable that uses this call when it detects RDP and then clear it when it becomes a local session. I might first look at using Win32 APIs to try and simulate a real keystroke though, as a real keystroke within 5 seconds of the screensaver starting does work. Many thanks for the suggestion though – Bryan Mar 20 '20 at 19:23

1 Answers1

0

You can use GetLastInputInfo from the WIN32 API (user32.dll) to figure out when the last user input occurred. After 10 mins of idle time (for instance), you can use the SendInput method (also from user32.dll) to send a 1 pixel mouse movement. This is interpreted as a physical movement and resets the last user input counter.

I've used this to write an app that stops the lock screen kicking in when people are on Zoom calls (it monitors for an active zoom call process).

https://www.pinvoke.net/default.aspx/user32/GetLastInputInfo.html https://www.pinvoke.net/default.aspx/user32/SendInput.html

MJM
  • 357
  • 1
  • 4
  • 16