Solved

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

Posted on 2000-04-11
13
878 Views
Last Modified: 2010-04-04
how can i gety the User ip number into a string value?
0
Comment
Question by:saar071697
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2703209
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
 
LVL 2

Expert Comment

by:craig_capel
ID: 2703334
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
 
LVL 1

Expert Comment

by:wmckie
ID: 2703680
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
 
LVL 2

Author Comment

by:saar071697
ID: 2707250
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
 
LVL 2

Expert Comment

by:craig_capel
ID: 2707678
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2708888
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:craig_capel
ID: 2708922
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
 
LVL 2

Author Comment

by:saar071697
ID: 2708985
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
 
LVL 2

Author Comment

by:saar071697
ID: 2708995
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
 
LVL 2

Expert Comment

by:craig_capel
ID: 2709145
saar, have u tried my code since i posted it?..... if not TRY it....
0
 
LVL 2

Accepted Solution

by:
craig_capel earned 75 total points
ID: 2709149


                    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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2709307
I think that's basically the same code as I dropped here earlier....
0
 
LVL 2

Author Comment

by:saar071697
ID: 2711157
Thanks! Now That Caught My Right IP Address
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now