Einzelnen Beitrag anzeigen

ByTheTime

Registriert seit: 24. Sep 2011
Ort: Frankfurt
297 Beiträge
 
Delphi XE2 Architect
 
#1

DNS Server ändern

  Alt 24. Mär 2013, 13:35
Moin,
ich versuche den DNS Server meines Netzwerkandapters aus meinem Programm heraus zu ändern, allerdings klappt das nicht so, wie ich es will:
Delphi-Quellcode:
uses
  ActiveX, UrlMon, Vcl.StdCtrls;


// ======================================================================
// SetDnsServers()
// Set DNS Servers via WMI
// Arguments ...
// APrimaryDNS - If Null String then DNS Server List is CLEARED.
// AAlternateDNS - [Optional]
//
// Return Values ...
// 0 Successful completion, no reboot required.
// 1 Successful completion, reboot required.
// -1 Unknown OLE Error
// 64 Method not supported on this platform.
// 65 Unknown failure.
// 66 Invalid subnet mask.
// 67 An error occurred while processing an instance that was returned.
// 68 Invalid input parameter.
// 69 More than five gateways specified.
// 70 Invalid IP address.
// 71 Invalid gateway IP address.
// 72 An error occurred while accessing the registry for the info.
// 73 Invalid domain name.
// 74 Invalid host name.
// 75 No primary or secondary WINS server defined.
// 76 Invalid file.
// 77 Invalid system path.
// 78 File copy failed.
// 79 Invalid security parameter.
// 80 Unable to configure TCP/IP service.
// 81 Unable to configure DHCP service.
// 82 Unable to renew DHCP lease.
// 83 Unable to release DHCP lease.
// 84 IP not enabled on adapter.
// 85 IPX not enabled on adapter.
// 86 Frame/network number bounds error.
// 87 Invalid frame type.
// 88 Invalid network number.
// 89 Duplicate network number.
// 90 Parameter out of bounds.
// 91 Access denied.
// 92 Out of memory.
// 93 Already exists.
// 94 Path, file, or object not found.
// 95 Unable to notify service.
// 96 Unable to notify DNS service.
// 97 Interface not configurable.
// 98 Not all DHCP leases could be released or renewed.
// 100 DHCP not enabled on adapter.
// ======================================================================

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;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(SetDnsServers('192.168.179.39')));
end;
Allerdings komme ich mit der Funktion nicht zurrecht. Ich habe als DNS Server 8.8.8.8 konfiguriert (Google). Fürhe ich mein Programm nun aus und klicke den Button (mit Adminrechten, sonst kommt Code 91 zurück) dann zeigt mir das Programm Code 0 an: Alles hat geklappt. Aber es passiert nichts, der DNS Server wird auf Standart zurückgesetzet, also 'DNS Serveradresse automatisch beziehen'.
Hat jemand da eine Idee?

Danke,
Lukas

[EDIT] Quelle
Lukas

Geändert von ByTheTime (24. Mär 2013 um 13:55 Uhr) Grund: Quelle angehängt!
  Mit Zitat antworten Zitat