Jump to content
Ian Branch

Detect if running in a remote session..

Recommended Posts

Hi Team,

I need to detect if my delphi (10.3.2) app is running in a remote RDP session.

I want to do the detection in the Project file so I can disable the Theme.

e.g.

  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  if not IsRemoteSession then TStyleManager.TrySetStyle('Iceberg Classico');

Thoughts/suggestions appreciated.

Regards & TIA,

Ian

Share this post


Link to post

Hi Fred,

Thanks for your suggestion..

Yes I have seen that and have tried a couple of things but none seem to work from the project file for one reason or another.

 

Ian

Share this post


Link to post

This works for me:

function DetectRemoteSession: boolean;
const
  SM_REMOTECONTROL      = $2001; // This system metric is used in a Terminal
                                 // Services environment. Its value is nonzero
                                 // if the current session is remotely
                                 // controlled; otherwise, 0.

  SM_REMOTESESSION      = $1000; // 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 Server console session, the
                                 // return value is 0. The console session is
                                 // not necessarily the physical console.
var
  Mode: string;
begin
  Result := (GetSystemMetrics(SM_REMOTESESSION) <> 0) or (GetSystemMetrics(SM_REMOTECONTROL) <> 0);

  // Test for emulated local/remote mode
  if (FindCmdLineSwitchEx('Session', Mode, ['-','\','/'], True)) then
  begin
    if (SameText(Mode, 'Remote')) then
      Result := True
    else
    if (SameText(Mode, 'Local')) then
      Result := False;
  end;
end;

 

  • Thanks 1

Share this post


Link to post
8 hours ago, Ian Branch said:

but none seem to work from the project file for one reason or another

Doesn't seem very plausible that this is the case. And it's tricky for us to dig deeper into "one reason or another". 

Share this post


Link to post

Hi Anders,

Thank you for your suggestion.  So, this is my implementation..

program LogViewer;

uses
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Themes,
  Vcl.Styles,
  System.SysUtils,
  MainFrm in 'MainFrm.pas' {MainForm};

{$R *.res}

function DetectRemoteSession: boolean;
const
  SM_REMOTECONTROL      = $2001; // This system metric is used in a Terminal
                                 // Services environment. Its value is nonzero
                                 // if the current session is remotely
                                 // controlled; otherwise, 0.

  SM_REMOTESESSION      = $1000; // 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 Server console session, the
                                 // return value is 0. The console session is
                                 // not necessarily the physical console.
var
  Mode: string;
begin
  Result := (GetSystemMetrics(SM_REMOTESESSION) <> 0) or (GetSystemMetrics(SM_REMOTECONTROL) <> 0);

  // Test for emulated local/remote mode
  if (FindCmdLineSwitchEx('Session', Mode, ['-','\','/'], True)) then
  begin
    if (SameText(Mode, 'Remote')) then
      Result := True
    else
    if (SameText(Mode, 'Local')) then
      Result := False;
  end;
end;

begin
  //
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  if not DetectRemoteSession then TStyleManager.TrySetStyle('Iceberg Classico');
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
  //
end.

Unfortunately, despite having vcl.controls in the uses, getsystemmetrics is still not recognised. :-(

Nor does it know anything about FindCmdLineSwitchEx

 

Thoughts/Suggestions?

 

Regards,

Ian

 

Share this post


Link to post

GetSystemMetrics is from Winapi.Windows. Use a find in files search of the rtl/vcl source to find which unit contains specific functions. 

Share this post


Link to post

There is no need for the function DetectRemoteSession to be in the DPR, it can just as easily be in MainFrm.pas,  just not in a method in a  form.  

 

Angus

 

Share this post


Link to post

FindCmdLineSwitchEx: Just replace  it with:

FindCmdLineSwitch('Session', Mode, True)

or simply remove the whole block. It's just for testing.

Share this post


Link to post

All,

Thank you for your input.  Much appreciated.

It is now working as desired.

 

Regards & Tks again.

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×