Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.757 Beiträge
 
Delphi 10.4 Sydney
 
#2

Re: IP über Delphi bestimmen!

  Alt 20. Jul 2006, 10:48
Kannst es ja mal damit versuchen.

Grüße
Klaus

Delphi-Quellcode:
function HostNameToIP(HostName: string): u_long;
  var
    RemoteHost: PHostEnt;
  begin
    result := u_long(INADDR_NONE);
    if HostName <> 'then
    try
      result := inet_addr(PChar(HostName)); // try a xxx.xxx.xxx.xxx first
      if result = u_long(INADDR_NONE) then
      begin
        RemoteHost := GetHostByName(PChar(HostName));
        if not ((RemoteHost = NIL) or (RemoteHost^.h_length <= 0)) then
          result := u_long(pointer(RemoteHost^.h_addr_list^)^);
      end;
    except
      result := u_long(INADDR_NONE);
    end;
  end;


  function OwnHostName: string;
  var
    buf : pointer;
    RemoteHost: PHostEnt;
  begin
    result := '';
    buf := NIL;
    try
      getmem(buf, 255);
      GetHostName(buf, 255);
      if char(buf^) <> #0 then
      begin
        RemoteHost := GetHostByName(buf);
        if RemoteHost <> NIL then result := RemoteHost^.h_name
                             else result := '127.0.0.1';
      end else result := '127.0.0.1';
    finally
      if buf <> NIL then FreeMem(buf, 255);
    end;
  end;


  function OwnIPAddress: u_long;
  begin
    result := HostNameToIP(OwnHostName);
  end;
oder hiermit:

Delphi-Quellcode:
function 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;
Klaus
  Mit Zitat antworten Zitat