Search code examples
delphiremote-desktoprdpmstsc

Delphi/Rdp check username and password before connect


as the subject say am trying to connect to server using this code in delphi

procedure TmainF.Button1Click(Sender: TObject);
var 
   rdp1 : TMsRdpClient7NotSafeForScripting ;
begin
  rdp1 := TMsRdpClient7NotSafeForScripting.Create(self);
  rdp1.Parent := mainF;
  rdp1.Server:=server_name;
  rdp1.UserName := user.Text;
  rdp1.AdvancedSettings7.ClearTextPassword := password.Text;
  rdp1.ConnectingText := 'connecting';
  rdp1.DisconnectedText := 'disconnected';
  rdp1.AdvancedSettings7.AuthenticationLevel:=0;
  rdp1.AdvancedSettings7.EnableCredSspSupport:=true;
  rdp1.Connect;
end;

the code is working fine but if the user entered a wrong user name or password
the rdp object is showing the remote desktop login prompt box to reenter the user name or password like the image

enter image description here

I want to validate the username and password before connect
or prevent this box and show a custom message from my app

I tried to use OnLogonError() procedure but it's not fired any code
and I read this Question but the code is c# and I confused with .getocx() and I can't find (PromptForCredentials) in (MSTSCLib_TLB.pas)

any help :( ??

sorry for my bad English.


Solution

  • There is an interface IMsRdpClientNonScriptable5 that has the following methods:

    • Get_AllowPromptingForCredentials()
    • Set_AllowPromptingForCredentials()

    If you call Set_AllowPromptingForCredentials() with the parameter set to false the dialog won't appear.

    As for how to get an instance of this interface - easy, you just cast the ControlInterface of your RDP client object:

    Ircns5 := rdp1.ControlInterface as IMsRdpClientNonScriptable5;
    if Assigned(Ircns5) then
      Ircns5.Set_AllowPromptingForCredentials(False);