Solved

How to detect the computer name ,username ,Ip

Posted on 2003-10-28
12
41,467 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

22 Experts available now in Live!

Get 1:1 Help Now