本文介绍了设置DNS地址时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一种流行的Delphi功能和一台带有Wi-fi适配器的笔记本电脑,试图设置首选和替代的dns服务器地址,并发现一条错误消息:无效语法。我认为 Win32_NetworkAdapterConfiguration功能仅在以太网适配器中有效,而在Wi-fi适配器中无效,或者我错了吗?
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?
生成的异常:
http://prntscr.com/4qvjmzhttp://prntscr.com/4qvjvn
这是函数:
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.
推荐答案
我用其他功能解决了我的问题,谢谢大家!
I solved my problem with other function, thank you all!
function ArrayToVarArray(Arr : Array Of string):OleVariant; overload;
var
i : integer;
begin
Result :=VarArrayCreate([0, High(Arr)], varVariant);
for i:=Low(Arr) to High(Arr) do
Result[i]:=Arr[i];
end;
procedure SetDNS();
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
vIPAddress : OleVariant;
begin
CoInitialize(nil);
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=True','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
vIPAddress := ArrayToVarArray(['104.131.220.61']);
if FWbemObject.SetDNSServerSearchOrder(vIPAddress) = 0 then
begin
exit;
end;
VarClear(vIPAddress);
FWbemObject:=Unassigned;
CoUninitialize;
end;
end;
这篇关于设置DNS地址时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!