设置DNS地址时出现问题

时间:2014-09-27 21:49:44

标签: delphi delphi-7

我使用了一个流行的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.

2 个答案:

答案 0 :(得分:2)

oDnsAddr未分配,因为iSize正在递增,但您的条件意味着它永远不会被填充。

if iSize = 0 then
begin
    oDnsAddr := VarArrayCreate([1,iSize],varOleStr);
    oDnsAddr[1] := APrimaryDNS;
    if iSize > 1 then oDnsAddr[2] := AAlternateDNS;
end;

然后你的后期条件得到满足

if iSize > 0 then
    Retvar := oNetAdapter.SetDNSServerSearchOrder(oDnsAddr)

提供未分配的OleVariant。

应该是:

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;

答案 1 :(得分:2)

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

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;