Solved

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

Posted on 2013-11-06
6
1,082 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 24

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 250 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 24

Assisted Solution

by:chaau
chaau earned 250 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 24

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 2 37
Not showing page correctly 3 30
Syntax Error 2 45
How do I prevent the pages in my site from showing up in a google search? 2 28
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now