gpt4 book ai didi

delphi - DNS地址设置问题

转载 作者:行者123 更新时间:2023-12-01 23:29:48 26 4
gpt4 key购买 nike

我正在使用一种流行的 Delphi 函数和一台连接了 Wi-Fi 适配器的笔记本电脑,尝试设置首选和备用 DNS 服务器地址并捕获错误消息 - “无效语法”。我认为“Win32_NetworkAdapterConfiguration”函数仅适用于以太网适配器,不适用于 Wi-fi 适配器,还是我错了?

生成异常:

http://prntscr.com/4qvjmz enter image description here http://prntscr.com/4qvjvn
enter image description here

这是函数:

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.

最佳答案

我用其他功能解决了我的问题,谢谢大家!

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;

关于delphi - DNS地址设置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com