Solved

How can i get a local ip address?

Posted on 2000-02-19
6
139 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 30 total points
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:dvd99
Comment Utility
Adjusted points to 30
0
 
LVL 1

Author Comment

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

Expert Comment

by:inthe
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

15 Experts available now in Live!

Get 1:1 Help Now