Hauptmenü
Home
Delphi
C# / .NET
Freepascal
Firebird
OPF
Tutorials
Tipps und Tricks
Links
Suche
Impressum
IP-Adresse des Rechners ermitteln PDF E-Mail
Geschrieben von Lemmy   
Sonntag, 19. Februar 2006
Die Funktion GetLocalIPs ermittelt alle aktuellen IP-Adressen im System. Die einzelnen Adressen sind durch Zeilenwechsel getrennt und können so etwa der Text-Eigenschaft eines TStrings-Objekts zugewiesen werden:

Uses Winsock, ...;

function TForm1.GetLocalIPs: String;
type PPInAddr= ^PInAddr;
var
  wsaData  : TWSAData;
  HostInfo : PHostEnt;
  HostName : Array[0..255] of Char;
  Addr     : PPInAddr;
begin
  Result:='';
  if WSAStartup($0102, wsaData) <> 0 then Exit;
  try
    if GetHostName(HostName, SizeOf(HostName)) <> 0 then Exit;
    HostInfo:= GetHostByName(HostName);
    if HostInfo=nil then Exit;
    Addr:=Pointer(HostInfo^.h_addr_list);
    if (Addr=nil) or (Addr^=nil) then Exit;
    Result:=StrPas(inet_ntoa(Addr^^));
    inc(Addr);
    while Addr^ <> nil do begin
      Result:=Result+^M^J+StrPas(inet_ntoa(Addr^^));
      inc(Addr);
    end;
  finally
    WSACleanup;
  end;
end;
 
Weiter >