• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 41540
  • Last Modified:

How to detect the computer name ,username ,Ip

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
baner
Asked:
baner
  • 4
  • 2
  • 2
  • +3
1 Solution
 
GloomyFriarCommented:
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
 
GloomyFriarCommented:
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
 
GloomyFriarCommented:
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
Independent Software Vendors: 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!

 
geobulCommented:
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
 
banerAuthor Commented:
sorry but I can`t do it
can you write some example please
0
 
banerAuthor Commented:
sorry Goe this is not for your answer
0
 
geobulCommented:
Why 'B' grade then? Full working code with an example how to use it. What more could I have added?
0
 
alsantosCommented:
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
 
alsantosCommented:
Sorry for comment after your answer geobul. I didn't see it.

alsantos
0
 
GloomyFriarCommented:
2 baner:
:(
0
 
bBKCommented:
Just to follow solution :)
0
 
akhachabCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now