Link to home
Start Free TrialLog in
Avatar of neox
neox

asked on

How to find out local IP in Delphi?

How to find out local IP in Delphi?
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlackDeath
BlackDeath

hi, inthe. my solution's mayb more readable; of corze ur's is nicer ;-) - this is 2 b seen as a comment, ok?

function Get_HostIP: string;
var
  HEnt: pHostEnt;
  WSAData: TWSAData;
  iCnt: Integer;
  sHostName: string;
  sIPAddr: string;
begin
  if WSAStartup($0101, WSAData) <> 0 then begin
    Result := 'WSAStartup error';
    Exit;
  end;
  try
    sIPAddr := '';
    sHostName := '';
    SetLength(sHostName, 100);
    if GetHostName(PChar(sHostName), 100) = 0 then begin
      SetLength(sHostName, StrLen(PChar(sHostName)));
      HEnt := GetHostByName(PChar(sHostName));
      if HEnt <> nil then begin
        for iCnt := 0 to HEnt^.h_length - 1 do
          sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_list^[iCnt])) + '.';
        SetLength(sIPAddr, Length(sIPAddr) - 1);
        Result := sIPaddr;
      end
      else
        Result := 'Hostname not found';
    end
    else case WSAGetLastError of
      WSAEFAULT        : Result := 'WSAEFault';
      WSANOTINITIALISED: Result := 'WSANotInitialised';
      WSAENETDOWN      : Result := 'WSAENetDown';
      WSAEINPROGRESS   : Result := 'WSAEInProgress';
    end;
  finally
    WSACleanup;
  end;
end;