远程登录环境检查

基于产品需要,需要程序检测当前运行环境是否处于远程登录。查阅相关资料得知,有以下方法。

基于SM_REMOTESESSION

通过GetSystemMetrics(SM_REMOTESESSION)方法,该方法的介绍如下:

int GetSystemMetrics(int nIndex); 获得指定的系统指标或者系统配置


SM_REMOTESESSION介绍

>This system metric is used in a Terminal Services environment. If the calling process is associated with a 
>Terminal Services client session, the return value is nonzero. If the calling process is associated with the
>Terminal Services console session, the return value is 0. 
>Windows Server 2003 and Windows XP:  The console session is not necessarily the physical console. 
>For more information, see WTSGetActiveConsoleSessionId.

如果调用者处于远程连接中,函数返回非0。如果处于本地连接中,返回0。官方文档上有提到,在Win8以及随后版本以及Windows Server 2012以及随后版本,远程连接协议RDP有改进,会把远程连接视为本地连接,这种情况下,上述方法检测无效。

基于注册表键值检测

读取位于HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\GlassSessionId的注册表键,该键值保存本地session的ID。如果当前sessionId与此值相等,则说明是本地连接登陆,否则是远程连接登陆。

但笔者通过远程连接,远程登陆到一台win7系统,在上述注册表位置中,没有找到 GlassSessionId内容,单独依赖注册表来判断也不准确。

综合方法一和方法二

先使用方法一,如果方法一返回为假,再使用方法二来判断。

参考链接:

  1. 检查当前是否远程桌面登录
  2. Detecting the Remote Desktop Services environment
上一篇:Linux - mac terminal快捷键 /mac终端快捷键


下一篇:Windows Terminal 新版将添加图形设置界面