Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can i get a local ip address?

Posted on 2000-02-19
6
Medium Priority
?
147 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
  • 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
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!

 
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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

916 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