Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Revert to Default DNS Nameservers

See more DNS Examples

Demonstrates how to revert back to the default nameservers of the local system where your application is running.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Dns;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  dns: TDns;
  nsCount: Integer;
  i: Integer;
  supportsTls: Boolean;

begin
  //  The only reason you would have to revert back to the local system's default nameservers
  //  is if your application had previously set different nameservers, and you wish to revert back.

  //  When your application starts, the DNS nameservers to be used are the default nameservers
  //  configured on the system where your application is running.

  //  There is one exception: Chilkat defaults to using the public Google nameservers (8.8.8.8 and 8.8.4.4) for
  //  Android applications. Reverting to the default nameservers would revert to the Google nameservers.

  //  However, Chilkat does not query the local system for the default nameservers unless Chilkat needs
  //  to do a DNS lookup and there not yet any DNS nameservers defined.  Thus technically, when your application
  //  starts, there are no default nameservers. They are auto-assigned when first needed.

  //  If your application explicitly defines DNS nameservers, such as by calling AddDefaultnamervers, or AddNameserver
  //  then Chilkat will not automatically auto-assign.

  dns := TDns.Create;

  //  Here we are explicitly adding the system default nameservers, because technically this application
  //  has not yet done anything that would require a DNS lookup, and thus DNS nameservers are not yet defined.
  //  Your default nameserver is assumed to not support TLS.
  dns.AddDefaultNameservers();

  //  Let's examine our nameservers
  nsCount := dns.NumNameservers;
  i := 0;
  while i < nsCount do
    begin
      WriteLn(i + 1 + ': ' + dns.GetNameserver(i));
      i := i + 1;
    end;

  //  On my Windows system, the result is a single nameserver at 
  //  1: 172.16.16.16

  //  ---------------------------------------------------------
  //  We could add additional nameservers, such as Google and Cloudfare
  supportsTls := True;
  //  Cloudfare
  dns.AddNameserver('1.1.1.1',supportsTls);
  //  Google
  dns.AddNameserver('8.8.8.8',supportsTls);

  //  See the nameservers now used by Chilkat
  nsCount := dns.NumNameservers;
  i := 0;
  while i < nsCount do
    begin
      WriteLn(i + 1 + ': ' + dns.GetNameserver(i));
      i := i + 1;
    end;

  //  Result:
  //  1: 172.16.16.16
  //  2: 1.1.1.1
  //  3: 8.8.8.8

  //  ---------------------------------------------------------
  //  To revert back to the system default nameservers, clear all
  //  then re-add the default.
  dns.RemoveAllNameservers();
  dns.AddDefaultNameservers();

  //  See the nameservers now used by Chilkat
  nsCount := dns.NumNameservers;
  i := 0;
  while i < nsCount do
    begin
      WriteLn(i + 1 + ': ' + dns.GetNameserver(i));
      i := i + 1;
    end;

  //  Result:
  //  1: 172.16.16.16


  dns.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.