// 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;
|
// 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;
|