Link to home
Start Free TrialLog in
Avatar of dabestprogrammerindauniverse
dabestprogrammerindauniverse

asked on

computer name

how do i get the computer name?
Avatar of Stuart_Johnson
Stuart_Johnson

It's found in the registry under:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName

HTH

Stu.
Avatar of dabestprogrammerindauniverse

ASKER

i mean how do i get it through programming?
ASKER CERTIFIED SOLUTION
Avatar of Stuart_Johnson
Stuart_Johnson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is this what you mean?

try the
getcomputername
windows api call, its in the halp.


I'm lazy so I stole this from
www.techtricks.com/delphi

procedure TForm1.Button1Click(Sender: TObject);
var
   pcName : array[0..MAX_COMPUTERNAME_LENGTH]of char;  
// Holds the computer name
   dwSize : dword;  
// length of the name
begin
      if not getComputerName( pcName, dwSize ) then
         application.MessageBox(
            pChar( 'Reason: The API call failed' + #10#10 +
                   'Windows reports the error code as ' +
                   intToStr( getLastError ) ),
                   'Can''t Get Computer Name', ID_OK )
      else
         application.MessageBox(
            pChar( 'This computer is named "' + pcName +
                   '", which contains ' + intToStr( dwSize ) +
                   ' character(s).' ), 'FYI', ID_OK );
end;

Dammit :)  I knew there was an API for it, I just couldn't remember what it was off the top of my head and did it the old fashioned way!
try this (i'm using Delphi 5)

procedure TForm1.FormCreate(Sender: TObject);
var s:array[0..20] of char; // should be long enough
    L:Cardinal;             // Longint in case Delphi 3
begin
  getComputerName(S,L);
  label1.Caption:=StrPas(s);
end;
Please do not accept the "answer" proposed by Sulistyo.  As you will notice, it is exactly the same as what alexstewart posted (only consensed).

Sulistyo - it is considered good manners to leave the question open and not lock it until asked to do so by the questioner.  This ensures that other experts get the oppotunity to post comments which maybe more useful that what you have proposed.

You will also note that you are using exactly the same API as alexstewart (GetComputerName).  Please make sure you read each comment posted thoroughly before posting an comment to make sure you aren't doubling up.

Stuart
I use 2 functions to get the computername or username (NT) :


function GetUserNameNT : String;
var
  PUserName: PChar;
  Size: LongWord;
begin
  Size:= 100;
  PUserName:= StrAlloc(100);
  if GetUserName(PUserName, Size) then
    Result := PUserName
  else
    Result := 'No user';
  StrDispose(PUserName);
end;


{ Get the ComputerName (Windows NT) }
function GetComputerNameNT : String;
var
  PComputerName: PChar;
  Size: LongWord;
begin
  Size:= 100;
  PComputerName:= StrAlloc(100);
  if GetComputerName(PComputerName, Size) then
    Result := PComputerName
  else
    Result := 'No ComputerName';
  StrDispose(PComputerName);
end;
Hi Stuart, I couldnt remember it either, its suprising how such an obvious name slips the mind.

Alex
wimmeyvaert,

I like your GetUserNameNT function. It also works on my Win 98 machine. Great!

Marcel (Holland)
Hi Marcel,

I hope 'dabestprogrammerindauniverse' also has this feeling, then maybe he'll grant me the points  ;-)

The Mayor.
dabestprogrammerindauniverse must be too busy coding to get back to us :)
Hi Alex,

It might be a good idea for you to post an answer - if only to lock this question.

Stu.
Umm.  Hi all.  I don't know why I was awarded the points for this.  IMO, they should have gone to Alex.  I've requested a reversal on the points, and also have them award to Alex.

https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20268370

Cheers all.

Stu.
Question for alex in this topic area for your assistance.

Thank you
Computer101
Community Support Moderator