检查当前是否远程桌面登录

嗯...嗯...基于某些不可描述的原因, 总之需要程序不允许在远程桌面登录时运行...-_-

查了一下资料, 挺简单的, 判断GetSystemMetrics(SM_REMOTESESSION)就可以

可是又看了下官网发现, 官方居然不推荐使用这方法了: https://docs.microsoft.com/en-us/windows/win32/termserv/detecting-the-terminal-services-environment

原因是2012使用了更NB的RDP协议, 导致这个方法不起作用...-_-

幸好官网上直接给出了示例, 照抄改成pascal的版本, 如下:

function IsRemoteable: Boolean;
var
  lR: TRegistry;
  lType, lGlassSessionId, lGSSize, lCurrentSessionId: DWORD;
begin
  Result := False;

  if GetSystemMetrics(SM_REMOTESESSION) = 0 then
    Exit;

  lR := TRegistry.Create(KEY_READ);
  try
    lR.RootKey := HKEY_LOCAL_MACHINE;
    if not lR.OpenKeyReadOnly('SYSTEMCurrentControlSetControlTerminal Server') then
      Exit;

    lGSSize := SizeOf(lGlassSessionId);
    if RegQueryValueEx(lR.CurrentKey, 'GlassSessionId', nil, @lType, PByte(@lGlassSessionId), @lGSSize) <> ERROR_SUCCESS then
      Exit;

    if not ProcessIdToSessionId(GetCurrentProcessId, lCurrentSessionId) then
      Exit;

    {相等是本地登录}
    if lCurrentSessionId = lGlassSessionId then
      Exit;
  finally
    lR.Free;
  end;

  Result := True;
end;
原文地址:https://www.cnblogs.com/lzl_17948876/p/13085867.html