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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/159910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:22:16  来源:igfitidea点击:

Determine if a program is running on a Remote Desktop

windowsterminal-servicescitrix

提问by Kluge

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 用户启用不活动超时。

采纳答案by Franci Penov

GetSystemMetrics(SM_REMOTESESSION) (as described in http://msdn.microsoft.com/en-us/library/aa380798.aspx)

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

回答by Ian Boyd

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

回答by Brian R. Bondy

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. Once you have the session ID you can use it to call: WTSQuerySessionInformation. You can specify WTSInfoClassas value WTSIsRemoteSessionand this will give you the information about if that application is a remote desktop connection or not.

您可以先通过调用ProcessIdToSessionId将进程 ID 转换为会话 ID 。获得会话 ID 后,您可以使用它来调用:WTSQuerySessionInformation。您可以指定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);
}