Solved

computername and ip

Posted on 2000-03-25
4
279 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 400 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

16 Experts available now in Live!

Get 1:1 Help Now