ScriptzRasDialer

From 40tude Dialog Wiki

zRasDialer.ds - Dialog include file to show a dialup dialog

See Handle dialup connections for details.



// ===========================================================================
// zRasDialer.ds
// Dialog include file to show a dialup dialog
// Version 2003-08-08 (Jürgen Haible, http://www.elbiah.de/tools/dialog/)
// ===========================================================================


// ---------------------------------------------------------------------------
// RasDialerEx: Show dialup dialog if no connection is already established
//    setConnection: Name of connection to pre-select ('' for none)
//    setUsername: Username to pre-set ('' for none)
//    setPassword: Password to pre-set ('' for none)
//    Returns: Identifier of established connection, 0 if not connected

function RasDialerEx( setConnection, setUsername, setPassword: String ): LongWord;
var  F: TForm;
     L: TLabel;
     B: TButton;
     cbConnection: TComboBox;
     emUsername: TEdit;
     emPassword: TEdit;
     RasList: TStringList;
begin
   // check if already connected
   Result := RasGetConnection;
   if Result <> 0 then exit; // yes, no need to dial

   // create form
   F := TForm.Create( Application );
   F.SetBounds( 229, 107, 281, 183 );
   F.Caption := 'Dialog Dialer';

   L := TLabel.Create( F );
   L.Parent := F;
   L.SetBounds( 12, 20, 57, 13 );
   L.Caption := 'Connection:';

   cbConnection := TComboBox.Create( F );
   cbConnection.Parent := F;
   cbConnection.SetBounds( 80, 16, 181, 21 );
   cbConnection.Style := csDropDownList;

   L := TLabel.Create( F );
   L.Parent := F;
   L.SetBounds( 12, 52, 51, 13 );
   L.Caption := 'Username:';

   emUsername := TEdit.Create( F );
   emUsername.Parent := F;
   emUsername.SetBounds( 80, 44, 181, 21 );

   L := TLabel.Create( F );
   L.Parent := F;
   L.SetBounds( 12, 80, 49, 13 );
   L.Caption := 'Password:';

   emPassword := TEdit.Create( F );
   emPassword.Parent := F;
   emPassword.SetBounds( 80, 72, 181, 21 );
   emPassword.PasswordChar := '#';

   B := TButton.Create( F );
   B.Parent := F;
   B.SetBounds( 56, 116, 75, 25 );
   B.Caption := 'OK';
   B.ModalResult := 1;

   B := TButton.Create( F );
   B.Parent := F;
   B.SetBounds( 140, 116, 75, 25 );
   B.Caption := 'Cancel';
   B.ModalResult := 2;

   try
      // fill combobox with available connections
      RasList := TStringList.Create();
      try
         RasEnumEntries( RasList );
         cbConnection.Items.Text := RasList.Text;
      finally
         RasList.Free;
      end;

      // pre-set given defaults
      cbConnection.ItemIndex := cbConnection.Items.IndexOf( setConnection );
      emUsername.Text := setUsername;
      emPassword.Text := setPassword;

      // show form
      if F.ShowModal = mrOk then begin

         // dial
         Result := RasDial( cbConnection.Text, emUsername.Text, emPassword.Text );

         if Result = 0 then begin
            Application.MessageBox( 'Dialing failed! Error: '
                + RasErrorText(RasLastError), 'Dial', 0 );
         end;

      end;

   finally
      F.Release;
   end;
end;


// ---------------------------------------------------------------------------
// RasDialer: Show dialup dialog if no connection is already established
//    Returns: True if connection is established now, False if not

function RasDialer: Boolean;
begin
   Result := ( RasDialerEx( '', '', '' ) <> 0 );
end;

// ===========================================================================


JuergenHaible