确定 Java 应用程序是否正在通过 RDP 会话运行?

发布于 2024-08-16 20:39:40 字数 106 浏览 4 评论 0原文

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

仅使用 Java 的解决方案是首选,但该应用程序保证可以在 Windows 上运行,所以我可以接受。

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

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

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

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

发布评论

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

评论(3

独享拥抱 2024-08-23 20:39:40

我认为您必须调用本机 Windows 库才能实现此目的。尝试这样的操作:

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,其中表示:

返回值

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

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

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

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.

故人的歌 2024-08-23 20:39:40

上述答案可能有效,但似乎不必要地复杂。您可以简单地读取 Windows 'sessionname' 环境变量来检测 RDP 会话。对于正常的本地会话,此环境变量的值将为“Console”。对于 RDP 会话,它将包含短语“RDP”。只需检查一下就很容易了。

public static boolean isRemoteDesktopSession() {
   System.getenv("sessionname").contains("RDP");
}

测试并确认在Windows7 64位下工作。我注意到这种技术的一个问题是,一旦 JVM 启动,从 System.getenv() 读取的环境变量值似乎不会改变。因此,如果 JVM 进程是由控制台会话启动,但随后由 RDP 会话访问,则进一步调用 System.getenv("sessionname") 仍会返回“Console”。

The above answers might work, but seem needlessly complicated. You can simply read the windows 'sessionname' environment variable to detect RDP sessions. The value of this environment variable will be 'Console' for a normal, local session. For an RDP session it will contain the phrase 'RDP'. It's easy enough just to check for that.

public static boolean isRemoteDesktopSession() {
   System.getenv("sessionname").contains("RDP");
}

Tested and confirmed working under Windows7 64bit. One issue I have noticed with this technique is that it appears that environment variable values as read from System.getenv() do not change once the JVM has started. So if the JVM process was started by a console session, but then accessed by an RDP session, further calls to System.getenv("sessionname") still return 'Console.'

潦草背影 2024-08-23 20:39:40

尝试使用 NativeCall ( http://johannburkard.de/software/nativecall/ )

您所需要的只是类路径中有 2 个 jar 加上 1 个 DLL。

快速测试:

import java.io.IOException;
import com.eaio.nativecall.*;

public class WindowsUtils {

public static final int SM_REMOTESESSION = 4096;  // remote session

  public static boolean isRemote() throws SecurityException, UnsatisfiedLinkError, 
  UnsupportedOperationException, IOException 
  {
    NativeCall.init();
    IntCall ic = null;
    ic = new IntCall("user32", "GetSystemMetrics"); 
    int rc = ic.executeCall(new Integer(SM_REMOTESESSION));
    if (ic != null) ic.destroy();
    return (rc > 0);
  }

  public static void main(String ... args) throws Exception {
    System.out.println(WindowsUtils.isRemote());
  }
}

Try with NativeCall ( http://johannburkard.de/software/nativecall/ )

All you need is 2 jars plus 1 DLL in your classpath.

A quick test :

import java.io.IOException;
import com.eaio.nativecall.*;

public class WindowsUtils {

public static final int SM_REMOTESESSION = 4096;  // remote session

  public static boolean isRemote() throws SecurityException, UnsatisfiedLinkError, 
  UnsupportedOperationException, IOException 
  {
    NativeCall.init();
    IntCall ic = null;
    ic = new IntCall("user32", "GetSystemMetrics"); 
    int rc = ic.executeCall(new Integer(SM_REMOTESESSION));
    if (ic != null) ic.destroy();
    return (rc > 0);
  }

  public static void main(String ... args) throws Exception {
    System.out.println(WindowsUtils.isRemote());
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文