Home Articles Books Downloads FAQs Tips

Q: Get the computer name


Answer

Call the GetComputerName API function.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
    char szBuf[MAX_COMPUTERNAME_LENGTH + 1];
    szBuf[0] = '\0';

    GetComputerName(szBuf, &dwSize);

    Label1->Caption = szBuf;
}

Note: The character buffer that you pass to GetComputerName must be large enough to hold MAX_COMPUTERNAME_LENGTH + 1 characters. The function call will fail if the buffer is smaller, even if the computer name is small enough to fit in your buffer.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.