How to find out local IP in Delphi?

How to find out local IP in Delphi?
neoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
procedure TForm1.Button1Click(Sender: TObject);
  { add to uses "winsock" }
var
  wVersionRequested : WORD;
  wsaData : TWSAData;
  p : PHostEnt;
  s : array[0..128] of char;
  p2 : pchar;
  OutPut:array[0..100] of char;
begin
{Start up WinSock}
     wVersionRequested := MAKEWORD(1, 1);
     WSAStartup(wVersionRequested, wsaData);

{Get the computer name}
     GetHostName(@s, 128);
     p := GetHostByName(@s);

{Get the IpAddress}
     p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
     StrPCopy(OutPut,'Hostname: '+Format('%s', [p^.h_Name])+#10#13+
                       'IPaddress: '+Format('%s',[p2])
                       );
     WSACleanup;
     MessageBox(0,OutPut,'NetInfo',mb_ok or mb_iconinformation);
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlackDeathCommented:
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;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.