Solved

How to detect the computer name ,username ,Ip

Posted on 2003-10-28
12
41,453 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry but I can`t do it
can you write some example please
0
 

Author Comment

by:baner
Comment Utility
sorry Goe this is not for your answer
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:geobul
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry for comment after your answer geobul. I didn't see it.

alsantos
0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
2 baner:
:(
0
 

Expert Comment

by:bBK
Comment Utility
Just to follow solution :)
0
 

Expert Comment

by:akhachab
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

8 Experts available now in Live!

Get 1:1 Help Now