?
Solved

Get HostName and Ip address of the machine that run the application

Posted on 2005-05-13
11
Medium Priority
?
418 Views
Last Modified: 2010-04-05
Hi Experts,
i need a way to get Hostname and ip address (when applicable) of the Computer that run my delphi application.

It must be full compatible with win 95/98/Nt/2000/XP.

Can anyone help me?

Best regards
0
Comment
Question by:balrom
  • 6
  • 4
11 Comments
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 500 total points
ID: 13994485
This will get your PC name and the local ip address of the PC

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.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;

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

end.
0
 

Author Comment

by:balrom
ID: 13994501
thank you mikelittlewood, but is this fully compatible with all windows version?

The documentation on msdn about gethostbyname tell that the function is deprecated.

Best regards
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13994514
Actually change the last bit for pc_name

Function TForm1.pc_name: string;
var
  i : cardinal;
  user:string;
begin
  i:=255;
  setlength(user,i);
  getComputername(pchar(user),i);
  Result := user;
end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13994522
Unfortunately I am at work at the moment so can only test on 2000/nt/xp and seems to work ok here.

95 and 98 you would need to test
0
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 13995008
that's not going to give the ISP IP if you're on dialup but if your behind a router, network, it's only going to show that, if you need the real IP then the only true way would be to connect to a webserver and request the info, which would indeed work on 9x, 2K, XP...

but of course you would be relying on the webserver being alive and a script, which would only take 5 minutes to do.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13995267
I thought he only wanted to ip address of the PC that is running his application?
0
 

Author Comment

by:balrom
ID: 13995327
Yes, i only want to retrive the ip address and hostname of the pc that is running th application...

I've tested the solution on a win 98 machine and seems to not work, but is a virtual machine.
We want to do the same test on a physical win98 machine, this require more time...

Best regards
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13995819
OK balrom, I will wait for your testing ...
0
 

Author Comment

by:balrom
ID: 13997822
ok mikelittlewood, i've tested on windows 98 and ME and works(!!!). Now i have to do the last test on a windows 95 machine, but on windows 95 i have another problem (i've just another post opened on this site) with indy components, i cannot test your function, because my application not starts.

Please, be patient

Best regards
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 14005124
No problem, glad it seems to be working so far.
0
 

Author Comment

by:balrom
ID: 14012512
Hi, I need more time to test on win95, i assume that works also on this system.

Thanks  mikelittlewood, only i have added a little imprevment on the gethostname function:

function geu_getHostName: String;
var
   i : cardinal;
   HostName:string;
begin
   i:=255;
   setlength(HostName,i);
   getComputername(pchar(HostName),i);
   Result := copy(HostName,1,i);                          // <<---the api return the length of the hostname in the 'i' variabile
end;

whithout this i always get a string of 255 characters long, padded on the right with random characters.
Of course, i need to know the real length of the hostname...

Best regards
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

862 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