Solved

How can i get a local ip address?

Posted on 2000-02-19
6
143 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 30 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
Industry Leaders: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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