Solved

computername and ip

Posted on 2000-03-25
4
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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