GetComputerNameExW

Retrieves the null-terminated name of a computer in one of the specified formats.

Requirements

Item Value Description
Minimum supported client Windows Vista [desktop apps only]
Minimum supported server Windows Server 2008 [desktop apps only]
Header winbase.h (include windows.h)
Library Kernel32.lib
DLL Kernel32.dll

Parameters

Parameter Type Description
NameFormat COMPUTER_NAME_FORMAT The format for the name. This parameter can be one of the following values from the COMPUTER_NAME_FORMAT enumeration.
lpBuffer LPWSTR A pointer to a buffer that receives the name. The name is null-terminated.
nSize LPDWORD A pointer to a DWORD value that specifies the size, in characters, of the buffer pointed to by lpBuffer. On input, this is the size of the buffer. On output, this is the number of characters written to the buffer, not including the terminating null character.

Return Value

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

If the buffer is not large enough, the function fails with ERROR_MORE_DATA and nSize is set to the required buffer size in characters, including the null terminator.

Remarks

To get the computer name in a specific format, pass the desired format as the NameFormat parameter.

The COMPUTER_NAME_FORMAT enumeration supports the following values:

  • ComputerNameNetBIOS: The NetBIOS name of the computer.
  • ComputerNameDnsHostname: The DNS host name of the computer.
  • ComputerNameDnsDomain: The DNS domain name of the computer.
  • ComputerNameDnsFullyQualified: The fully qualified DNS name of the computer.
  • ComputerNamePhysicalNetBIOS: The NetBIOS name of the physical computer (not a virtual machine).
  • ComputerNamePhysicalDnsHostname: The DNS host name of the physical computer (not a virtual machine).
  • ComputerNamePhysicalDnsDomain: The DNS domain name of the physical computer (not a virtual machine).
  • ComputerNamePhysicalDnsFullyQualified: The fully qualified DNS name of the physical computer (not a virtual machine).

Call GetComputerNameExW twice. The first call should be to determine the buffer size required by passing NULL for lpBuffer and 0 for nSize. The second call should be to retrieve the actual name. However, if you are confident in a reasonable buffer size (e.g., 256 characters), you can pass that size directly to avoid a second call.

Example


#include <windows.h>
#include <stdio.h>

int main() {
    WCHAR buffer[256];
    DWORD size = 256;

    if (GetComputerNameExW(ComputerNameDnsFullyQualified, buffer, &size)) {
        wprintf(L"Fully Qualified DNS Name: %s\n", buffer);
    } else {
        wprintf(L"Error getting computer name. Error code: %lu\n", GetLastError());
        return 1;
    }

    size = 256; // Reset size for next call
    if (GetComputerNameExW(ComputerNameNetBIOS, buffer, &size)) {
        wprintf(L"NetBIOS Name: %s\n", buffer);
    } else {
        wprintf(L"Error getting computer name. Error code: %lu\n", GetLastError());
        return 1;
    }

    return 0;
}