Vinit Sankhe
Vinit Sankhe

Reputation: 19885

WPF identify remote access (Not Winforms)

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

Upvotes: 0

Views: 542

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292575

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;
}

Upvotes: 3

Bas
Bas

Reputation: 27105

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;
        }
    }
}

Upvotes: 1

Related Questions