确定 Java 应用程序是否正在通过 RDP 会话运行? [英] Determine if Java App is being run over an RDP Session?

查看:48
本文介绍了确定 Java 应用程序是否正在通过 RDP 会话运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测我的 Swing 应用程序是否正在从 Windows RDP 会话运行?

How can I detect if my Swing App is being run from a windows RDP session?

首选仅 Java 解决方案,但该应用程序保证可以在 Windows 上运行,因此我可以进行脱壳.

Java only solution preferred, but the app is guaranteed to be running on windows so I'm ok with shelling out.

推荐答案

我认为您必须调用本机 Windows 库才能实现这一点.尝试这样的事情:

I think you'll have to invoke the native Windows libraries to pull this off. Try something like this:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.*; 
import com.sun.jna.examples.win32.Kernel32;

...

public static boolean isLocalSession() {
  Kernel32 kernel32;
  IntByReference pSessionId;
  int consoleSessionId;
  Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
  pSessionId = new IntByReference();

  if (lib.ProcessIdToSessionId(lib.GetCurrentProcessId(), pSessionId)) {
    consoleSessionId = lib.WTSGetActiveConsoleSessionId();
    return (consoleSessionId != 0xFFFFFFFF && consoleSessionId == pSessionId.getValue());
  } else return false;
}

consoleSessionId 看起来很奇怪的条件来自 WTSGetActiveConsoleSessionId,上面写着:

That strange-looking condition for consoleSessionId is from the documentation for WTSGetActiveConsoleSessionId, which says:

附加到物理控制台的会话的会话标识符.如果没有会话附加到物理控制台(例如,如果物理控制台会话正在附加或分离的过程中),则此函数返回 0xFFFFFFFF.

Return Value

The session identifier of the session that is attached to the physical console. If there is no session attached to the physical console, (for example, if the physical console session is in the process of being attached or detached), this function returns 0xFFFFFFFF.

这篇关于确定 Java 应用程序是否正在通过 RDP 会话运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆