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

发布于 2024-07-07 06:37:00 字数 148 浏览 8 评论 0原文

有没有办法让我的程序可以确定它何时在远程桌面(终端服务)上运行?

我想在程序在远程桌面会话上运行时启用“不活动超时”。 由于用户因保持远程桌面会话打开而臭名昭著,因此我希望我的程序在指定的不活动时间后终止。 但是,我不希望为非 RD 用户启用不活动超时。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苄①跕圉湢 2024-07-14 06:37:00

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

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

童话 2024-07-14 06:37:00

这是我使用的 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;
    }
}

Here's the C# managed code i use:

/// <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;
    }
}
空城缀染半城烟沙 2024-07-14 06:37:00

如果您想了解在您的会话中运行的应用程序,则以下内容有效:

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

但通常不适用于任何进程 ID。


如果您想了解可以在任意会话中运行的任意进程,那么您可以使用以下方法。

您可以首先通过调用 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);
}

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.


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 WTSInfoClass as value WTSIsRemoteSession and this will give you the information about if that application is a remote desktop connection or not.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文