?
Solved

computername and ip

Posted on 2000-03-25
4
Medium Priority
?
290 Views
Last Modified: 2010-05-18
I need to get the computername and ip in a delphiprogram.
ex.
myip := ?????????
cname := ?????????
0
Comment
Question by:karelin
  • 3
4 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 1600 total points
ID: 2657454
hello
here are some functions:
Regards Barry

computername:

procedure TForm1.Button1Click(Sender: TObject);
var
i : cardinal;
user:string;
begin
i:=255;
setlength(user,i);
getusername(pchar(user),i); // or GetComputerName
setlength(user,(i-1));
showmessage(user);

end;


ip address to use with name from above:

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;

function GetHostName(name : string) : string;
var
wsdata : TWSAData;
  he : PHostEnt;
  ip : TInAddr;           //   194.151.190.8
begin
  WSAStartup(MakeWord(1, 1), wsdata);
  ip.S_addr := inet_addr(PChar(name));
  he := gethostbyaddr(@ip.S_un_b, 4, AF_INET);        
  if he<>nil then
    Result := string(he.h_name);
  WSACleanup();
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text := GetIPAddress(Edit1.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit1.Text := GetHostName(Edit2.Text);
end;
0
 
LVL 17

Expert Comment

by:inthe
ID: 2657470
just to note the first function was for getusername but is same for getcomputername and should be like below:

procedure TForm1.Button1Click(Sender: TObject);
var
i : cardinal;
user:string;
begin
i:=255;
setlength(user,i);
//i replaced GetUserName with
GetComputerName
GetComputerName(pchar(user),i);
setlength(user,(i-1));
showmessage(user);
end;


0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2657500
Ok, now all-in-one  :o)


unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

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);
var cname: array[0..MAX_COMPUTERNAME_LENGTH] of Char;
    csize: Cardinal;
begin
  csize := MAX_COMPUTERNAME_LENGTH + 1;
  GetComputerName(cname, csize);
  Edit1.Text := cname;
  Edit2.Text := GetIPAddress(cname);
end;

end.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2657676
yes dont forget to add "winsock" to your uses section when using the getipaddress function.
0

Featured Post

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!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 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