I'm using one popular Delphi function and one laptop with Wi-fi adapter connected on attempt to set preferred and alternative dns server address and catch an error saying - "Invalid Syntax". I think "Win32_NetworkAdapterConfiguration" function work only in Ethernet adapters and not in Wi-fi adapters, or I'm wrong?
Exceptions generated:
http://prntscr.com/4qvjmz
http://prntscr.com/4qvjvn
And this is the function:
uses
ComObj, ActiveX, UrlMon, Variants;
function SetDnsServers(const APrimaryDNS : string;
const AAlternateDNS : string = '') : integer;
var Retvar : integer;
oBindObj : IDispatch;
oNetAdapters,oNetAdapter,
oDnsAddr,oWMIService : OleVariant;
i,iValue,iSize : longword;
oEnum : IEnumvariant;
oCtx : IBindCtx;
oMk : IMoniker;
sFileObj : widestring;
begin
Retvar := 0;
sFileObj := 'winmgmts:\\.\root\cimv2';
iSize := 0;
if APrimaryDNS <> '' then inc(iSize);
if AAlternateDNS <> '' then inc(iSize);
if iSize = 0 then begin
oDnsAddr := VarArrayCreate([1,iSize],varOleStr);
oDnsAddr[1] := APrimaryDNS;
if iSize > 1 then oDnsAddr[2] := AAlternateDNS;
end;
OleCheck(CreateBindCtx(0,oCtx));
OleCheck(MkParseDisplayNameEx(oCtx,PWideChar(sFileObj),i,oMk));
OleCheck(oMk.BindToObject(oCtx,nil,IUnknown,oBindObj));
oWMIService := oBindObj;
oNetAdapters := oWMIService.ExecQuery('Select * from ' +
'Win32_NetworkAdapterConfiguration ' +
'where IPEnabled=TRUE');
oEnum := IUnknown(oNetAdapters._NewEnum) as IEnumVariant;
while oEnum.Next(1,oNetAdapter,iValue) = 0 do begin
try
if iSize > 0 then
Retvar := oNetAdapter.SetDNSServerSearchOrder(oDnsAddr)
else
Retvar := oNetAdapter.SetDNSServerSearchOrder();
except
Retvar := -1;
end;
oNetAdapter := Unassigned;
end;
oDnsAddr := Unassigned;
oNetAdapters := Unassigned;
oWMIService := Unassigned;
Result := Retvar;
end;
//Usage:
begin
SetDnsServers('104.131.220.61','104.131.220.61');
end.