Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can i get a local ip address?

Posted on 2000-02-19
6
Medium Priority
?
146 Views
Last Modified: 2010-04-04
Hello in my application i would like to be able to show the local systems ip address in a edit box. any ideas?
Thanks!
0
Comment
Question by:dvd99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 120 total points
ID: 2539496
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);
begin
  Edit1.Text := GetIPAddress(your_pc's_name);
end;

you could use getcomputername api call to fill the your_pc's_name part..or just type it( the name of your pc) in the code..
0
 
LVL 1

Author Comment

by:dvd99
ID: 2541173
great could you please tell me were to put the top part of the code?
And what do I do with the your_pc's_name
the same program needs to show my friends ip address when he runs it on his pc.
Thanks!
0
 
LVL 17

Expert Comment

by:inthe
ID: 2543637
"were to put the top part of the code"
anywherein iplementation of your code(above the button click for instance.)


your pc name  = name of your pc ..

use getcomputername() win api call to fill it.

for more points i'd give a complete working unit for any pc..

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dvd99
ID: 2544614
Adjusted points to 30
0
 
LVL 1

Author Comment

by:dvd99
ID: 2544615
30 points is all i have, sorry but i don't think that enough.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2547656
Here is full unit:
(note to add Winsock in uses section).


unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function pc_name : string;
var
i : cardinal;
user:string;
begin
i:=255;
setlength(user,i);
getComputername(pchar(user),i);
setlength(user,(i-1));
end;

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);
begin
  Edit1.Text := GetIPAddress(pc_name);
end;

end.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

721 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