Click to Rate and Give Feedback
MSDN
MSDN Library
Windows Development
System Services
System Information
 GetComputerName function
GetComputerName function

Applies to: desktop apps only

Retrieves the NetBIOS name of the local computer. This name is established at system startup, when the system reads it from the registry.

GetComputerName retrieves only the NetBIOS name of the local computer. To retrieve the DNS host name, DNS domain name, or the fully qualified DNS name, call the GetComputerNameEx function. Additional information is provided by the IADsADSystemInfo interface.

The behavior of this function can be affected if the local computer is a node in a cluster. For more information, see ResUtilGetEnvironmentWithNetName and UseNetworkName.

Syntax

BOOL WINAPI GetComputerName(
  __out    LPTSTR lpBuffer,
  __inout  LPDWORD lpnSize
);

Parameters

lpBuffer [out]

A pointer to a buffer that receives the computer name or the cluster virtual server name. The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.

lpnSize [in, out]

On input, specifies the size of the buffer, in TCHARs. On output, the number of TCHARs copied to the destination buffer, not including the terminating null character.

If the buffer is too small, the function fails and GetLastError returns ERROR_BUFFER_OVERFLOW. The lpnSize parameter specifies the size of the buffer required, including the terminating null character.

Return value

If the function succeeds, the return value is a nonzero value.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The GetComputerName function retrieves the NetBIOS name established at system startup. Name changes made by the SetComputerName or SetComputerNameEx functions do not take effect until the user restarts the computer.

If the caller is running under a client session, this function returns the server name. To retrieve the client name, use the WTSQuerySessionInformation function.

Examples

For an example, see Getting System Information.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

GetComputerNameW (Unicode) and GetComputerNameA (ANSI)

See also

Computer Names
GetComputerNameEx
SetComputerName
SetComputerNameEx
System Information Functions

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Why are you posting p/invoke signatures?      Ugarte   |   Edit   |   Show History
System.Environment.MachineName just p/invokes this function, so there's not really any good reason to roll your own.
Tags What's this?: Add a tag
Flag as ContentBug
The ANSI version does not return the required length      Florin Crişan ... Stanley Roark   |   Edit   |   Show History
I have to maintain an old application that uses ANSI.
I have noticed that, while the Unicode version returns the required buffer length, the ANSI version does not.
So, on my Windows XP SP2 machine, this code prints 8:
DWORD computerNameLength = 0;
GetComputerNameW(0, &;;computerNameLength);
cout << computerNameLength << endl;
But if I change it to use GetComputerNameA, it prints 0.
Did anybody else notice this?
The ANSI version does not return the required length      pankajadlakha ... Stanley Roark   |   Edit   |   Show History
I am also noticing something similar. GetComputerNameA does not return the required length, if the input length (second param) is 0 or less than the required length, the output length(second param) is just the same as input length. Also if GetComputerNameW is used in the following way:

DWORD dwCompNameLen = 0;
GetComputerNameW(0, &;;dwCompNameLen);

the value of dwCompNameLen includes the terminating NULL character too, which is not expected.

Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker