Thema: Delphi NameServer IP ermitteln

Einzelnen Beitrag anzeigen

Ati

Registriert seit: 16. Okt 2003
Ort: Gelsenkirchen
406 Beiträge
 
Delphi 3 Professional
 
#2

Re: NameServer IP ermitteln

  Alt 25. Jan 2006, 15:48
Um das nochmal heimlich zu pushen, möchte ich den gefunden Code hier mal reinschreiben. Vielleicht weiß ja jemand warum das bei mir nicht geht oder wie ich was anders schrieben muß.
Delphi-Quellcode:
const
  MAX_ADAPTER_DESCRIPTION_LENGTH = 128; // arb.
  MAX_ADAPTER_NAME_LENGTH = 256; // arb.
  MAX_ADAPTER_ADDRESS_LENGTH = 8; // arb.

type
  PIP_ADDRESS_STRING = ^IP_ADDRESS_STRING;
  IP_ADDRESS_STRING = packed record
    acString : array [1..16] of Char;
  end;

  PIP_MASK_STRING = ^PIP_MASK_STRING;
  IP_MASK_STRING = IP_ADDRESS_STRING;

  PIP_ADDR_STRING = ^IP_ADDR_STRING;
  IP_ADDR_STRING = packed record
    Next : PIP_ADDR_STRING;
    IpAddress : IP_ADDRESS_STRING;
    IpMask : IP_MASK_STRING;
    Context : DWORD;
  end;

  time_t = comp;


  PIP_ADAPTER_INFO = ^IP_ADAPTER_INFO;
  IP_ADAPTER_INFO = packed record
    Next : PIP_ADAPTER_INFO;
    ComboIndex : DWORD;
    AdapterName : array [1..MAX_ADAPTER_NAME_LENGTH+4] of Char ;
    Description : array [1..MAX_ADAPTER_DESCRIPTION_LENGTH+4] of Char;
    AddressLength : UINT;
    Address : array [1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
    Index : DWORD;
    dwType : UINT;
    DhcpEnabled : UINT;
    CurrentIpAddress : PIP_ADDR_STRING;
    IpAddressList : IP_ADDR_STRING;
    GatewayList : IP_ADDR_STRING;
    DhcpServer : IP_ADDR_STRING;
    HaveWins : Boolean;
    PrimaryWinsServer : IP_ADDR_STRING;
    SecondaryWinsServer : IP_ADDR_STRING;
    LeaseObtained : time_t;
    LeaseExpires : time_t;
  end;

  PIP_PER_ADAPTER_INFO = ^IP_PER_ADAPTER_INFO;
  IP_PER_ADAPTER_INFO = packed record
    AutoConfigEnabled : UINT;
    AutoConfigActive : UINT;
    CurrentDnsServer : PIP_ADDR_STRING;
    DnsServerList : IP_ADDR_STRING;
  end;


function GetAdaptersInfo(const pAdapterInfo : PIP_ADAPTER_INFO;const pOutBufLen : PULONG) : DWORD; stdcall;
function GetPerAdapterInfo(const pIfIndex : ULONG;const pAdapterInfo : PIP_PER_ADAPTER_INFO;const pOutBufLen : PULONG) : DWORD; stdcall;




implementation


function GetAdaptersInfo; external 'iphlpapi.dll';
function GetPerAdapterInfo; external 'iphlpapi.dll';



{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  pAdapterList : PIP_ADAPTER_INFO;
  dwLenAdapter : DWORD;
  aaiAdapters : array of PIP_ADAPTER_INFO; //Hier die beschriebene Fehlermeldung
  dwResult : DWORD;
  iIndexAdapter : integer;
  iasWork : IP_ADDR_STRING;
  paiWork : PIP_PER_ADAPTER_INFO;
  dwLenAdapterInfo : DWORD;


begin
  pAdapterList := nil; // Alles auf 0 ==> Benötigte Buffergrösse ermitteln
  dwLenAdapter := 0;
  if GetAdaptersInfo(pAdapterList,@dwLenAdapter) <> ERROR_BUFFER_OVERFLOW then exit;
  pAdapterList := AllocMem(dwLenAdapter);
  try
    if GetAdaptersInfo(pAdapterList,@dwLenAdapter) <> ERROR_SUCCESS then exit;
    SetLength(aaiAdapters,1);
    aaiAdapters[0] := pAdapterList;
    while aaiAdapters[High(aaiAdapters)].Next <> nil do begin
      SetLength(aaiAdapters,Length(aaiAdapters)+1);
      aaiAdapters[High(aaiAdapters)] := aaiAdapters[High(aaiAdapters)-1].Next;
    end;
    for iIndexAdapter := 0 to high(aaiAdapters) do begin
      ListBox1.Items.Add(trim(aaiAdapters[iIndexAdapter].Description)+'/'+trim(aaiAdapters[iIndexAdapter].AdapterName));
      paiWork := nil;
      dwLenAdapterInfo := 0;
      dwResult := GetPerAdapterInfo(aaiAdapters[iIndexAdapter].Index,paiWork,@dwLenAdapterInfo);
      if dwResult <> ERROR_BUFFER_OVERFLOW then begin
        ListBox1.Items.Add('***** (1) Fehler beim Auslesen der Adapterdaten: '+IntToStr(dwResult));
      end else begin
        paiWork := AllocMem(dwLenAdapterInfo);
        try
          dwResult := GetPerAdapterInfo(aaiAdapters[iIndexAdapter].Index,paiWork,@dwLenAdapterInfo);
          if dwResult <> ERROR_SUCCESS then begin
            ListBox1.Items.Add('***** (2) Fehler beim Auslesen der Adapterdaten: '+IntToStr(dwResult));
          end else begin
            iasWork := paiWork.DnsServerList;
            while iasWork.Next <> nil do begin
              ListBox1.Items.Add('DNS: '+trim(iasWork.IpAddress.acString));
              iasWork := iasWork.Next^;
            end;
            ListBox1.Items.Add('DNS:'+trim(iasWork.IpAddress.acString));
          end;
        finally
          FreeMem(paiWork,dwLenAdapterInfo);
        end;
      end;
    end;
  finally
    FreeMem(pAdapterList,dwLenAdapter);
  end;
end;

end.
Ich weiß das ich nichts weiß

Sokrates
  Mit Zitat antworten Zitat