0

I have some code in a standard WPF program in which I want to know whether the computer on which my program runs is remote controlled.

For this I use SystemParameters.IsRemoteControlled as was suggested in this answer.

SystemParameters.IsRemoteControlled: true if the current session is remotely controlled; otherwise, false. This system metric is used in a Terminal Services environment. Maps to SM_REMOTECONTROL. See GetSystemMetrics.

private bool IsRemoteControlled => SystemParameter.IsRemoteControlled();

Alas, if I start my program while I use Remote Desktop or TeamViewer to control the program, the value is false, even if I run the program on a computer without a monitor.

Using GetSystemMetrics in a console program, doesn't help, I still get false.

static void Main(string[] args)
{
    bool isRemoteControlled = GetIsRemoteControlled();
    Console.WriteLine(isRemoteControlled);
}

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetSystemMetrics(SystemMetric smIndex);

private static bool GetTerminalServerSession()
{
    const int SM_REMOTECONTROL = 0x2001;
    int result = GetSystemMetrics(SM_REMOTECONTROL);
    bool isRemoteSession = (result != 0);
    return isRemoteSession;
}

So how do I know whether the computer is remote controlled, using remote desktop, and preferably using any remote control program. What event is associated with the value change?

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • Note that the documentation clearly says that "This system metric is used in a Terminal Services environment." Neither Remote Desktop nor TeamViewer are using Terminal Services. – Cody Gray - on strike Jan 17 '21 at 10:22
  • `SM_REMOTESESSION` will work if you want to detect Remote Desktop; see https://learn.microsoft.com/en-us/windows/win32/termserv/detecting-the-terminal-services-environment – Cody Gray - on strike Jan 17 '21 at 10:24

0 Answers0