Einzelnen Beitrag anzeigen

Passi077

Registriert seit: 7. Okt 2004
23 Beiträge
 
#1

IP Adresse eines Netzwerkadapters

  Alt 8. Mai 2006, 16:01
Hi,

ich will die aktuelle IP Adresse von allen verfügbaren Netzwerkadaptern auslesen. Durch die Forensuche bin ich dann an die Funktion GetPerAdapterInfo gestoßen und habe aich ein paar Code Beispiele gefunden:

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 = int64;

  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;
  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));
      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;

Dieser Code gibt alle Adapter inkl. DNS aus. Nun brauche ich aber noch die aktuelle IP. Da CurrentIpAddress aber vom Typ PIP_ADDR_STRING ist, weiß ich nicht wie ich diesen Wert ausgebe (was bedeutet das Dach..?).. trim funktioniert hier nicht..

Kann mir jemand helfen?

Vielen Dank + Grüße
Passi
  Mit Zitat antworten Zitat