Solved

computername and ip

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 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