檢查當前是否遠程桌面登錄


嗯...嗯...基於某些不可描述的原因, 總之需要程序不允許在遠程桌面登錄時運行...-_-

查了一下資料, 挺簡單的, 判斷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('SYSTEM\CurrentControlSet\Control\Terminal 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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM