Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to detect the computer name ,username ,Ip

Posted on 2003-10-28
12
Medium Priority
?
41,535 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Accepted Solution

by:
geobul earned 375 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

609 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