Solved

How to detect the computer name ,username ,Ip

Posted on 2003-10-28
12
41,493 Views
Last Modified: 2012-08-13
Hi experts ,
I need to know which comuter sending a information to the server.
and my question is :
how to detect the  comuter name ,user name or IP address in Labeal for example

thanks
 
0
Comment
Question by:baner
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9633157
GetComputerName
The GetComputerName function retrieves the NetBIOS name of the local computer. This name is established at system startup, when the system reads it from the registry.

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9633171
GetUserName
The GetUserName function retrieves the user name of the current thread. This is the name of the user currently logged onto the system.

Windows 2000/XP: Use the GetUserNameEx function to retrieve the user name in a specified format. Additional information is provided by the IADsADSystemInfo interface.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9633234
A bit harder with ip...
Each IP address and subnet mask pair is stored in the registry entries IPAddress and SubnetMask, in the subkey HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \Services \Tcpip \Parameters\Interfaces\<adapter>.

But i'm not sure that it is the best way.
0
Industry Leaders: 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!

 
LVL 17

Accepted Solution

by:
geobul earned 125 total points
ID: 9633417
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses WinSock;

function GetLocalIPAddress : string;
var wsdata : TWSAData;
    he : PHostEnt;
    ss : pchar;
    ip : TInAddr;
    i  : cardinal;
    co : string;
begin
  i := MAX_COMPUTERNAME_LENGTH + 1;
  SetLength(co,i);
  GetComputerName(PChar(co),i);
  WSAStartup(MakeWord(1, 1), wsdata);
  he := gethostbyname(pchar(co));
  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 GetLocalName(var sUser,sComputer : string);
var
  i : cardinal;
begin
  try
    i:=255;
    { user }
    SetLength(sUser,i);
    GetUserName(PChar(sUser),i);
    SetLength(sUser,(i));
    { computer }
    i := 255;
    SetLength(sComputer,i);
    GetComputerName(PChar(sComputer),i);
    SetLength(sComputer,(i));
  except
    ShowMessage('Can not get Local Name !');
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var s1,s2: string;
begin
  GetLocalName(s1,s2);
  Edit1.Text := s2; // computer
  Edit2.Text := s1; // user
  Edit3.Text := GetLocalIPAddress; // IP address
end;

end.

Regards, Geo
0
 

Author Comment

by:baner
ID: 9633832
sorry but I can`t do it
can you write some example please
0
 

Author Comment

by:baner
ID: 9633880
sorry Goe this is not for your answer
0
 
LVL 17

Expert Comment

by:geobul
ID: 9633974
Why 'B' grade then? Full working code with an example how to use it. What more could I have added?
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9634017
Hi baner, You can use this, you need 3 Edit and 2 Button:

uses Winsock;

// Button1 OnClick Get the ComputerName and IP
procedure TForm1.Button1Click(Sender: TObject);
var
   WSAData: TWSAData;
   HostAddress: PHostEnt;
   Name, Address : string;
begin
  WSAStartup(2, WSAData);
  SetLength(Name, 255);
  Gethostname(PChar(Name), 255);  
  SetLength(Name, StrLen(PChar(Name)));
  Edit1.Text:=Name;  // put the computername on Edit
  HostAddress := gethostbyname(PChar(Name));
  with HostAddress^  do
    Address := Format('%d.%d.%d.%d',[
  Byte(h_addr^[0]),
  Byte(h_addr^[1]),
  Byte(h_addr^[2]),
  Byte(h_addr^[3])]);
  WSACleanup;
  edit2.text:=address; // put the IP on Edit2
end;

// Button2 On CLick get the current username
procedure TForm1.Button2Click(Sender: TObject);
const
  cnMaxNameLen = 254;
var
  sName: string;
  dwNameLen: DWORD;
begin
  dwNameLen := cnMaxNameLen - 1;
  SetLength(sName, cnMaxNameLen);
  GetUserName(PChar(sName), dwNameLen);
  SetLength(sName, dwNameLen);
  result := UpperCase(Trim(sName));
end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9634054
Sorry for comment after your answer geobul. I didn't see it.

alsantos
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9634823
2 baner:
:(
0
 

Expert Comment

by:bBK
ID: 10456851
Just to follow solution :)
0
 

Expert Comment

by:akhachab
ID: 11627964
try


Function GetUser : string;
 Var
     buffer : String;
     buffsize : DWORD;
  Begin
       buffsize := 128;
       Setlength(buffer,buffsize);
       Getusername(Pchar(buffer),buffsize);
       result := buffer;
  end;


-username
Function GetUser : string;
 Var
     buffer : String;
     buffsize : DWORD;
  Begin
       buffsize := 128;
       Setlength(buffer,buffsize);
       Getusername(Pchar(buffer),buffsize);
       result := buffer;
  end;
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := GetUser;
end;
------------------------------------------------
Function pcname:string;
var
  arrCompName : Array[0..255] Of Char;
  strCompName : String;
  CompNameLen : Cardinal;
begin
 CompNameLen := MAX_COMPUTERNAME_LENGTH + 1;
  If GetComputerName(arrCompName, CompNameLen) Then
    Begin
     SetLength(strCompName, MAX_COMPUTERNAME_LENGTH + 1);
    Result := StrPas(arrCompName);
end;


function GetIP: string;
var
ipwsa:TWSAData; p:PHostEnt; s:array[0..128] of char; c:pchar;
begin
wsastartup(257,ipwsa);
GetHostName(@s, 128);
p := GetHostByName(@s);
c := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
Result := String(c);
end;

and you do Label.caption:= ....
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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