Solved

computer name

Posted on 2001-09-10
15
233 Views
Last Modified: 2010-04-06
how do i get the computer name?
0
Comment
  • 7
  • 2
  • 2
  • +4
15 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6472988
It's found in the registry under:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName

HTH

Stu.
0
 

Author Comment

by:dabestprogrammerindauniverse
ID: 6473039
i mean how do i get it through programming?
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 50 total points
ID: 6473056
uses Registry;

function GetComputerName: String;
begin
  with TRegistry.Create do
    try
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey('SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName', false) then
  if ValueExists('ComputerName') then
    Result := ReadString('ComputerName');
      CloseKey;
    finally
      free; {TRegistry}
    end; {try..finally}
end; {GetComputerName}
0
 
LVL 2

Expert Comment

by:alexstewart@beta
ID: 6473107
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;

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6473121
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!
0
 

Expert Comment

by:Sulistyo
ID: 6473187
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;
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6473234
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
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 6473971
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;
0
 
LVL 2

Expert Comment

by:alexstewart@beta
ID: 6476819
Hi Stuart, I couldnt remember it either, its suprising how such an obvious name slips the mind.

Alex
0
 
LVL 1

Expert Comment

by:mpoots
ID: 6477479
wimmeyvaert,

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

Marcel (Holland)
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 6478521
Hi Marcel,

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

The Mayor.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6478561
dabestprogrammerindauniverse must be too busy coding to get back to us :)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6486277
Hi Alex,

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

Stu.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6809573
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.

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

Cheers all.

Stu.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6809726
Question for alex in this topic area for your assistance.

Thank you
Computer101
Community Support Moderator
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

19 Experts available now in Live!

Get 1:1 Help Now