?
Solved

How to find out local IP in Delphi?

Posted on 1999-11-26
2
Medium Priority
?
334 Views
Last Modified: 2010-04-04
How to find out local IP in Delphi?
0
Comment
Question by:neox
2 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 20 total points
ID: 2236840
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
 
LVL 1

Expert Comment

by:BlackDeath
ID: 2243192
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question