0

How can a WPF app know if its getting remotely operated (via VNC or remote desktop)?

In winforms there is System.Windows.Forms.SystemInformation.TerminalServerSession as per Detecting remote desktop connection but is there a strightforward way for this in WPF?

I guess the hack for now could be to have an invisible Winforms host on WPF and use its own capacity to host dummy win form that can identify the same... but that looks lame to me!

Any inputs would be appreciated!

Thx

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71

2 Answers2

3

I guess the hack for now could be to have an invisible Winforms host on WPF and use its own capacity to host dummy win form that can identify the same... but that looks lame to me!

You don't need an invisible WinForms host... you can just add a reference to the System.Windows.Forms assembly, and use the SystemInformation.TerminalServerSession static property.

If you don't want a dependency on WinForms, you can use the GetSystemMetrics Win32 API:

const int SM_REMOTESESSION = 0x1000;

[DllImport("user32")]
static extern int GetSystemMetrics(int nIndex);


public static bool IsTerminalServerSession()
{
    return (GetSystemMetrics(SM_REMOTESESSION) & 1) != 0;
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

This method does not require a Windows Forms Window, only a reference to the DLL.

If you don't want to reference this, you can call the method to check this yourself, the implementation is as follows (I've wrapped it in a class):

static class SystemInformation
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern int GetSystemMetrics(int nIndex);
    public static bool IsTerminalServerSession
    {
        get
        {
            //copied the Windows Forms implementation
            return (GetSystemMetrics(0x1000) & 1) != 0;
        }
    }
}
Bas
  • 26,772
  • 8
  • 53
  • 86