how can i get the user's i.p into a string value?

how can i gety the User ip number into a string value?
LVL 2
saar071697Asked:
Who is Participating?
 
craig_capelConnect With a Mentor Commented:


                    unit Unit1;

                    interface

                    uses
                      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, winsock,
                      StdCtrls;

                       type
                                          TName = array[0..100] of Char;
                                          PName = ^TName;

                    type
                      TForm1 = class(TForm)
                        Button1: TButton;
                        Edit1: TEdit;
                        Edit2: TEdit;
                        procedure Button1Click(Sender: TObject);
                      private
                        { Private declarations }
                      public
                        { Public declarations }
                      end;

                    var
                      Form1: TForm1;
                          sIP, sHost, sErr: string;
                       buffer: array[0..50] of char;

                       function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;


                    implementation

                    {$R *.DFM}

                       function GetHostIP(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;

                    procedure TForm1.Button1Click(Sender: TObject);
                     begin
                                          if GetHostIP(sHost, sIP, sErr) then begin
                                            Edit1.Text := sHost;
                                            Edit2.Text := sIP;
                                          end

                    end;

                    end.

This Code!
0
 
EpsylonCommented:
Try this:

uses Winsock;

function GetIPAddress(name : string) : string;
var wsdata : TWSAData;
    he : PHostEnt;
    ss : pchar;
    ip : TInAddr;
begin
  WSAStartup(MakeWord(1, 1), wsdata);
  he := gethostbyname(pchar(name));
  if he<>nil then
  begin
    ip.S_addr := integer(pointer(he^. h_addr_list^)^);
    ss := inet_ntoa(ip);
    Result := string(ss);
  end;
  WSACleanup();
end;

procedure TForm1.Button1Click(Sender: TObject);
var a: array[0..MAX_COMPUTERNAME_LENGTH+1] of Char;
    l: Cardinal;
begin
  l := MAX_COMPUTERNAME_LENGTH;
  GetComputerName(a, l);
  Edit1.Text := GetIPAddress(a);
end;


Regards,

Epsylon.
0
 
craig_capelCommented:
there is only one floor with this, and i also pointed this out to Barry, if you were to take this program anywhere else, it would not work... u need to know the username... and even then on a network, it returns the network address...

unit ipaddy1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, winsock,
  StdCtrls, ExtCtrls, Psock;


type
  TName = array[0..100] of Char;
  PName = ^TName;


type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Timer1: TTimer;
    Edit3: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    NMFTP1: TNMFTP;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NMFTP1Connect(Sender: TObject);
    procedure NMFTP1Success(Trans_Type: TCmdType);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;



var
  Form1: TForm1;
  sIP, sHost, sErr: string;
   buffer: array[0..50] of char;
  p: trect;
  ftpconnect,changedir,connected: boolean;
  p3,p4: trect;
  n: integer;
  result: byte;
  counter: longint;
  wnd,wnd2: hwnd;
  p2: tpoint;
  i1 : integer;
  r1: trect;
  tmpname,tmpclass,s1 : string;

 function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;


implementation

{$R *.DFM}


function GetHostIP(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;


procedure TForm1.FormActivate(Sender: TObject);
begin
  if GetHostIP(sHost, sIP, sErr) then begin
    Edit1.Text := sHost;
    Edit2.Text := sIP;
  end

end;


end.


sorry, i quickly stripped out of the code u did not.... The vars, you will have to sort out yourself.....

This code will return the ip address of the machine on the internet, even if its on a network and without the use of a username....
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
wmckieCommented:
An alternative to the two previous answers is to make use of the IP Helper API (IPHLPAPI.DLL) but its use is dependant on the Windows version, its supported in Win2000 & 98 but not in Win95, I think NT4 has limited support depentant on service pack.

However, the following works on my Win95 system, I think that the IPHLPAPI.DLL was installed by either WinSock2 or IE5.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, WinSock;

type
  PMIB_IPADDRROW = ^TMIB_IPADDRROW;
  TMIB_IPADDRROW = record
                     dwAddr: dword;
                     dwIndex: dword;
                     dwMask: dword;
                     dwBCastAddr: dword;
                     dwReasmSize: dword;
                     unused1: word;
                     unused2: word;
                   end;  { TMIB_IPADDRROW }

  PMIB_IPADDRTABLE = ^TMIB_IPADDRTABLE;
  TMIB_IPADDRTABLE = record
                       dwNumEntries: dword;
                       Table: array[0 .. 0] of TMIB_IPADDRROW;
                     end; { TMIB_IPADDRTABLE }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function GetIpAddrTable(pIpAddrTable: PMIB_IPADDRTABLE;
                        var pdwSize:  dword;
                        bOrder: BOOL): dword; stdcall; external 'IPHLPAPI.DLL';

procedure TForm1.Button1Click(Sender: TObject);
var
  IpAddrTable: PMIB_IPADDRTABLE;
  Size, dwResult: dword;
  ErrorMessage: array[0 .. 256] of char;
  LibH: THandle;
  C: word;
begin
  { Confirm that the IP Helper API DLL exists }
  LibH := LoadLibrary('IPHLPAPI.DLL');
  if LibH = 0
    then Exit
    else FreeLibrary(LibH);
  Memo1.Lines.Clear;

  Size := 1;
  GetMem(IpAddrTable, Size);
  FillChar(IpAddrTable^, Size, #0);
  dwResult := GetIpAddrTable(IpAddrTable, Size, true);
  if dwResult = ERROR_INSUFFICIENT_BUFFER
    then
      begin
        FreeMem(IpAddrTable);
        GetMem(IpAddrTable, Size);
        FillChar(IpAddrTable^, Size, #0);
        dwResult := GetIpAddrTable(IpAddrTable, Size, true);
      end
    else
      begin
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, dwResult, 0,
                      @ErrorMessage, Sizeof(ErrorMessage) - 1, nil);
        Memo1.Lines.Add('Function GetIpAddrTable failed - ' + ErrorMessage);
      end;

  { Display all IP addresses }
  for C := 0 to IpAddrTable.dwNumEntries - 1 do
    Memo1.Lines.Add(inet_ntoa(in_addr(IpAddrTable.Table[C].dwAddr)));

  FreeMem(IpAddrTable);
end;

end.

Cheers - Walter McKie
0
 
saar071697Author Commented:
Epsylon : I tried Your Way But It's getting me the worng ip address
craig_capel : my delphi 4 cannot find something that calls NMFTP.
wmckie : I Need this program that will work on win95 platform

please try to help me with other ways.

0
 
craig_capelCommented:
Saar...

NMFTP is a component, nothing to do with what u need, i was in a hurry.... Do this....

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, winsock,
  StdCtrls;

   type
                      TName = array[0..100] of Char;
                      PName = ^TName;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
      sIP, sHost, sErr: string;
   buffer: array[0..50] of char;

   function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;


implementation

{$R *.DFM}

   function GetHostIP(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;

procedure TForm1.Button1Click(Sender: TObject);
 begin
                      if GetHostIP(sHost, sIP, sErr) then begin
                        Edit1.Text := sHost;
                        Edit2.Text := sIP;
                      end

end;

end.





That will work....
0
 
EpsylonCommented:
Saar, what do you mean with the wrong IP address?

Btw, a user can't have an IP address, only a computer can have 1 or more IP addresses.
0
 
craig_capelCommented:
Becuase my computer is on the network it has more than 1 ip...

192.168.0.1 (Network)
then 195.18.23.56 (This changes on mine everytime i log on, unless u have a static ip)

yeah, the code i had was not pulling the ip address correctly (except the code i posted :) )

IT was pulling out the network ip not the internet ip, i think he may mean the same....
0
 
saar071697Author Commented:
Epsylon: i ment about the user's computer but the wrong ip is that the ip i got was something with 169 and my real ip was 194.90.229.3
0
 
saar071697Author Commented:
my ip is for network and internet try to ping 194.90.229.3 this is my server that i can get to him with the network(that without internet)  and without the network
0
 
craig_capelCommented:
saar, have u tried my code since i posted it?..... if not TRY it....
0
 
EpsylonCommented:
I think that's basically the same code as I dropped here earlier....
0
 
saar071697Author Commented:
Thanks! Now That Caught My Right IP Address
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.