Solved

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

Posted on 2013-11-06
6
1,218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 25

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 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