How to obtain/set the search order of DNS servers?
This page presents the two Delphi functions to read and change the search order for DNS servers. The DNS servers are used by TCP/IP protocol during name resolution process. These examples utilize TWmiQuery, TWmiMethod and TWmiConnection components. TWmiConnection component is used to connect to the destination host. TWmiQuery component locates the specified network adapter. TWmiMethod component executes SetDNSServerSearchOrder method of Win32_NetworkAdapterConfiguration class. The source code below creates and destroys the components on the fly, so these functions may be used in console- or UI-type applications.
How to get the search order of DNS servers?  Download

// this function returns a list of DNS domain servers that are assigned 
// to a specified network adapter. These servers are used for name resolution. 
// The method may be used for local or remote host. When used for local host 
// Destination, UserName and Password parameters must be empty. 
// For remote computer, the parameters must be:  
// TargetHost: name or IP address of the remote host, like '10.8.36.54' 
// UserName: name of the user, may include domain, like 'MOON\Administrator';  
// Password: the user's password. 
// Adapter: the caption of the adapter where DHCP is to be enabled. 
//          ListActiveAdapters function returns this caption. 
//          See "How to find IP enabled adapter?" topic. 
// Servers: receives an ordered list of current DNS servers. 
procedure GetDnsServerSearchOrder(Destination, UserName, Password, Adapter: string; Servers: TStrings);
var
  vConfigs: TWmiQuery;
  vConnection: TWmiConnection;
  vLow, vHigh, i: integer;
  vResult: Variant;
begin
  vConfigs     := TWmiQuery.Create(nil);
  vConnection  := TWmiConnection.Create(nil);
  try
    vConnection.MachineName := Destination;
    vConnection.Credentials.UserName := UserName;
    vConnection.Credentials.Password := Password;

    // try to connect. It may fail if the destination computer does not have 
    // WMI (like windows 95, 98, NT without WMI core installed), or 
    // if the provided credentials are not valid 
    vConnection.Connected := true;

    vConfigs.WQL.Text  := 'select * from Win32_NetworkAdapterConfiguration ';
    vConfigs.Connection := vConnection;
    vConfigs.Open;

    if not vConfigs.Locate('Caption', Adapter, []) then
      raise Exception.Create('Network adapter not found: ' + Adapter);

    vResult := vConfigs.FieldByname('DnsServerSearchOrder').AsVariant;
    if VarIsArray(vResult) then
    begin
      vLow := VarArrayLowBound(vResult, 1);
      vHigh := VarArrayHighBound(vResult, 1);
      for i := vLow to vHigh do Servers.Add(vResult[i]);
    end else
    if not VarIsEmpty(vResult) then Servers.Add(vResult);

  finally
    vConfigs.Free;
    vConnection.Free;
  end;
end;

How to change the search order of DNS servers?  Download

// this function changes the list of DNS servers that are assigned 
// to a specified network adapter. These servers are used for name resolution. 
// The method may be used for local or remote host. When used for local host 
// Destination, UserName and Password parameters must be empty. 
// For remote computer, the parameters must be:  
// TargetHost: name or IP address of the remote host, like '10.8.36.54'  
// UserName: name of the user, may include domain, like 'MOON\Administrator'; 
// Password: the user's password. 
// Adapter: the caption of the adapter where DHCP is to be enabled. 
//          ListActiveAdapters function returns this caption. 
//          See "How to find IP enabled adapter?" topic. 
// Servers: An ordered list of new domain servers addresses, each line contains one IP address 
procedure ChangeDnsServerSearchOrder(Destination, UserName, Password, Adapter: string; Servers: TStrings);
var
  vConfigs: TWmiQuery;
  vConnection: TWmiConnection;
  vMethod: TWmiMethod;
  i: integer;
  vParam: Variant;
  s: string;
begin
  vConfigs     := TWmiQuery.Create(nil);
  vConnection  := TWmiConnection.Create(nil);
  vMethod      := TWmiMethod.Create(nil);
  try
    vConnection.MachineName := Destination;
    vConnection.Credentials.UserName := UserName;
    vConnection.Credentials.Password := Password;

    // try to connect. It may fail if the destination computer does not have
    // WMI (like windows 95, 98, NT without WMI core installed), or
    // if the provided credentials are not valid
    vConnection.Connected := true;

    vConfigs.WQL.Text  := 'select * from Win32_NetworkAdapterConfiguration ';
    vConfigs.Connection := vConnection;
    vConfigs.Open;
    vMethod.WmiObjectSource := vConfigs;

    if not vConfigs.Locate('Caption', Adapter, []) then
      raise Exception.Create('Network adapter not found: ' + Adapter);

    vMethod.WmiObjectSource := vConfigs;
    vMethod.WmiMethodName := 'SetDNSServerSearchOrder';
    if (Servers <> nil) and (Servers.Count > 0) then
    begin
      vParam := VarArrayCreate([0, Servers.Count - 1], varVariant);
      for i := 0 to Servers.Count - 1 do vParam[i] := Servers[i];
      vMethod.InParams.ParamByName('DNSServerSearchOrder').Value := vParam;
    end;  

    if vMethod.Execute() <> 0 then
    begin
      s := vMethod.LastWmiErrorDescription;
      if s = '' then s := 'Error ' + IntToStr(vMethod.LastWmiError);
      raise Exception.Create(s);
    end;
  finally
    vMethod.Free;
    vConfigs.Free;
    vConnection.Free;
  end;
end;