Link to home
Start Free TrialLog in
Avatar of ShadowHawk071998
ShadowHawk071998

asked on

How to find my IP address?

Hi Guys.
I want to find my IP address. That is, the computer's address. Is there an API to do it?
It seems so trivial but yet I couldn't find it...

Tsh
Avatar of ECollin
ECollin

Hi,

this is a sample code to retrieve your IP adress and the computer name


Uses Winsock;

procedure TForm1.FormCreate(Sender: TObject);
var wVersionRequested : WORD;
wsaData : TWSAData;
begin
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
end;

procedure TForm1.Button1Click(Sender: TObject);
var p : PHostEnt; s : array[0..128] of char; p2 : pchar;
begin
GetHostName(@s, 128);
p := GetHostByName(@s);
Memo1.Lines.Add(p^.h_Name);
{Get the IpAddress}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
Memo1.Lines.Add(p2);

end;


Emmanuel
this is another solution i've built some time ago (just for completion):

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;

@least, there's error handling and the wsacleanup...

;-)

regards,

BlackDeath.
listening.

by the way, get ready that you'll be encountering problems with machines more than 1 network adapter. (i.e. you have an ethernet card and a modem, so you won't be getting info about your internet IP because the ethernet has a static IP.  I've found a function which gives the Online IP and it works well but it gives my static ip when i'm offline and the internet ip when i'm connected.

finally i've found a solution but using another component..

cheers
listening :-)

Hi mahara... what 'other component' are you using now to get your IP that avoids the ethernet card prob???
'127.0.0.1' is your IP address and 'localhost' is the hostname   :o)
hey gwena is here.. the author of nice online IP, i remember you :))))

the magras component. it gets the ip number of the dial-up adapter. i've tested and it works.

Well, i cannot read other comments in detail because of little time, so if this is actually the same with any other, sorry.

But this function gives the dial-up adapter's IP address first (if available). So it can be used in conjunction with a dial-up connection check or whatever to make use.

here is the code.

// add those types
type
  TName = array[0..100] of Char;
  PName = ^TName;


// here is the function
function TheInternetIp(var sHostName, sIPAddr, sWSAError: string): Boolean;
var
  HEnt: pHostEnt;
  HName: PName;
  WSAData: TWSAData;
  iCnt: Integer;
begin
  Result := False;
  if WSAStartup($0101, WSAData) <> 0 then begin
    sWSAError := 'WSAStartup error';
    Exit;
  end;
  sHostName := '';
  sIPAddr := '';
  sWSAError := '';
  New(HName);
  if GetHostName(HName^, SizeOf(TName)) = 0 then begin
    Result := True;
    sHostName := StrPas(HName^);
    HEnt := GetHostByName(HName^);
    for iCnt := 0 to HEnt^.h_length - 1 do
      sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_list^[iCnt])) + '.';
    SetLength(sIPAddr, Length(sIPAddr) - 1);
  end
  else begin
    case WSAGetLastError of
      WSAEFAULT        : sWSAError := 'WSAEFault';
      WSANOTINITIALISED: sWSAError := 'WSANotInitialised';
      WSAENETDOWN      : sWSAError := 'WSAENetDown';
      WSAEINPROGRESS   : sWSAError := 'WSAEInProgress';
    end;
  end;
  Dispose(HName);
  WSACleanup;
end;


// add those vars
  shost, sip, serror : string;

// and call the function to
// evaluate the vals.
TheInternetIP(shost, sip, serror);


try:


add a  TPowerSock and then do:

AnsiString ip = Powersock1->LocalIP;




This is C++Builder code but it should be +- the same in delphi.



    Good luck

Filipe correia
listenning
Avatar of ShadowHawk071998

ASKER

I need help here...
I took a look, and tried Mahara's proposal, and it worked.
But I never tried ECollins answer, so maybe he's right too.

What do the EE policy (or ethics) say? SHould I reject ECollins and let Mahara answer???

Tsh.
ECollins code works too but is not complete. But BlackDeath's code works too and seems complete to me.

So I think you have to choose between 3 people....
Hi ShadowHawk
here is another solution.

uses WinSock...;
function LocalIP : string;
type
    TaPInAddr = array [0..10] of PInAddr;
    PaPInAddr = ^TaPInAddr;
var
    phe  : PHostEnt;
    pptr : PaPInAddr;
    Buffer : array [0..63] of char;
    I    : Integer;
    GInitData      : TWSADATA;

begin
    WSAStartup($101, GInitData);
    Result := '';
    GetHostName(Buffer, SizeOf(Buffer));
    phe :=GetHostByName(buffer);
    if phe = nil then Exit;
    pptr := PaPInAddr(Phe^.h_addr_list);
    I := 0;
    while pptr^[I] <> nil do begin
      result:=StrPas(inet_ntoa(pptr^[I]^));
      Inc(I);
    end;
    WSACleanup;
end;
well, it will work with all - but try others with an eth. adapter!

but i think that the one i've posted has more than the proposed answer.

cheers!
well. I guess I'll give the points to mahara, sorry to all that posted. I didn't have the time to try it all, and Mahar's idea worked, and worked great.

Sorry,

Tsh.
ASKER CERTIFIED SOLUTION
Avatar of mahara
mahara

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
Answer accepted