Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to detect if a running Windows application is being accessed remotely?

Posted on 2013-11-06
6
Medium Priority
?
1,391 Views
Last Modified: 2013-11-15
Hi Experts,

I am looking for a way to detect if my running vb.net Windows Forms application is being accessed remotely via software such as Remote Desktop, VNC, Teamviewer etc.

Regards,
Leigh
0
Comment
Question by:LeighWardle
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39629238
There is a Win32 API that is designed for this:
GetSystemMetrics(SM_REMOTESESSION);

Open in new window

When the program running in a remote desktop session the API will return not zero value.
Please check this web-site how to call this API via PInvoke:
Public Declare Auto Function GetSystemMetrics Lib "user32.dll" (ByVal smIndex As Integer) As Integer

Open in new window

0
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 1000 total points
ID: 39638009
hi... Try this

using System.Runtime.InteropServices;


 public class RemoteSessionChecker
  {
    /// <summary>
    /// Checks whether the current user is is logged in through a remote session
    /// </summary>
    /// <returns></returns>
    public static bool Check()
    {
      bool IsRemoteSession = false;
      //Constant used to check for remote sessions
      const int SM_REMOTESESSION = 0x2001;
      //This function will return 0 if the session is local
      int Value = GetSystemMetrics(SM_REMOTESESSION);

      IsRemoteSession = (Value != 0);

      return IsRemoteSession;
    }

    [DllImport("user32.dll", EntryPoint = ("GetSystemMetrics"))]
    private static extern int GetSystemMetrics(int nIndex);
  }
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 39640517
Hi samirbhogayta,  thanks for your suggestion.

I am not a c## developer, so I converted your code to vb.net:

Imports System.Runtime.InteropServices

Public Class RemoteSessionChecker
	''' <summary>
	''' Checks whether the current user is is logged in through a remote session
	''' </summary>
	''' <returns></returns>
	Public Shared Function Check() As Boolean
		Dim IsRemoteSession As Boolean = False
		'Constant used to check for remote sessions
		Const  SM_REMOTESESSION As Integer = &H2001
		'This function will return 0 if the session is local
		Dim Value As Integer = GetSystemMetrics(SM_REMOTESESSION)

		IsRemoteSession = (Value <> 0)

		Return IsRemoteSession
	End Function

	<DllImport("user32.dll", EntryPoint := ("GetSystemMetrics"))> _
	Private Shared Function GetSystemMetrics(nIndex As Integer) As Integer
	End Function
End Class

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

Open in new window


But the function always returns False - even when I am connected by RDP.

Regards,
Leigh
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 25

Assisted Solution

by:chaau
chaau earned 1000 total points
ID: 39640547
SM_REMOTESESSION value is 0x1000, or converting to VB it is &H1000
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 39640593
Thanks, chaau.

Changing SM_REMOTESESSION value (for VB) to &H1000 now makes the code detect if it is connected by RDP.
That's the good news.

The bad news is that it does not detect if it is connected via TeamViewer.  (I haven't tried VNC or anything else).
0
 
LVL 25

Expert Comment

by:chaau
ID: 39640648
Teamviewer, Logmein and such technically do not work via RDP, but connect to the user session. They cannot be detected via SM_REMOTESESSION. You will need to test if processes associated with these services are running using CreateToolhelp32Snapshot() API.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question