Solved

computername and ip

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 238
control image tags in a string ? 12 142
Delphi...Split view - idea? 1 89
How to build JSON File in Delphi 6 3 45
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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