简体   繁体   English

确定程序是否在远程桌面上运行

[英]Determine if a program is running on a Remote Desktop

Is there a way my program can determine when it's running on a Remote Desktop (Terminal Services)? 我的程序有没有办法确定它何时在远程桌面(终端服务)上运行?

I'd like to enable an "inactivity timeout" on the program when it's running on a Remote Desktop session. 我想在程序在远程桌面会话上运行时启用“不活动超时”。 Since users are notorious for leaving Remote Desktop sessions open, I want my program to terminate after a specified period of inactivity. 由于用户因打开远程桌面会话而臭名昭着,我希望我的程序在指定的不活动时间后终止。 But, I don't want the inactivity timeout enabled for non-RD users. 但是,我不希望为非RD用户启用非活动超时。

GetSystemMetrics(SM_REMOTESESSION)(如http://msdn.microsoft.com/en-us/library/aa380798.aspx中所述

Here's the C# managed code i use: 这是我使用的C#托管代码:

/// <summary>
/// Indicates if we're running in a remote desktop session.
/// If we are, then you MUST disable animations and double buffering i.e. Pay your taxes!
/// 
/// </summary>
/// <returns></returns>
public static Boolean IsRemoteSession
{
    //This is just a friendly wrapper around the built-in way
    get
    {
        return System.Windows.Forms.SystemInformation.TerminalServerSession;
    }
}

The following works if you want to know about YOUR application which is running in YOUR session: 如果您想了解在您的会话中运行的应用程序,以下内容有效:

BOOL IsRemoteSession(void)
{
   return GetSystemMetrics( SM_REMOTESESSION );
}

But not in general for any process ID. 但不是一般的任何进程ID。


If you want to know about any arbitrary process which could be running in any arbitrary session then you can use the below method. 如果您想知道任何可以在任意会话中运行的任意进程,那么您可以使用以下方法。

You can first convert the process ID to a session ID by calling ProcessIdToSessionId . 您可以通过调用ProcessIdToSessionId将进程ID转换为会话ID。 Once you have the session ID you can use it to call: WTSQuerySessionInformation . 获得会话ID后,您可以使用它来调用: WTSQuerySessionInformation You can specify WTSInfoClass as value WTSIsRemoteSession and this will give you the information about if that application is a remote desktop connection or not. 您可以将WTSInfoClass指定为值WTSIsRemoteSession ,这将为您提供有关该应用程序是否是远程桌面连接的信息。

BOOL IsRemoteSession(DWORD sessionID)
{
   //In case WTSIsRemoteSession is not defined for you it is value 29
   return WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, sessionID, WTSIsRemoteSession, NULL, NULL);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM