Error 234 "More data is available" with GetComputerNameEx
c, string, winapi
Solution
Per the `GetComputerNameEx()` documentation:
lpBuffer [out] A pointer to a buffer that receives the computer name or the cluster virtual server name.
The length of the name may be greater than `MAX_COMPUTERNAME_LENGTH` characters because DNS allows longer names. To ensure that this buffer is large enough, set this parameter to `NULL` and use the required buffer size returned in the `lpnSize` parameter.
lpnSize [in, out] On input, specifies the size of the buffer, in `TCHAR`s. On output, receives the number of `TCHAR`s copied to the destination buffer, not including the terminating null character.
If the buffer is too small, the function fails and `GetLastError` returns `ERROR_MORE_DATA`. This parameter receives the size of the buffer required, including the terminating null character.
If `lpBuffer` is NULL, this parameter must be zero.
For example:
int wmain()
{
COMPUTER_NAME_FORMAT nameType = ComputerNameDnsFullyQualified;
WCHAR *computerName = NULL, *computerNameNew;
DWORD size = 0;
BOOL pcName;
DWORD error;
do
{
pcName = GetComputerNameExW(nameType, computerName, &size);
if (pcName) break;
error = GetLastError();
if (error != ERROR_MORE_DATA) break;
computerNameNew = (WCHAR*) realloc(computerName, sizeof(WCHAR) * size);
if (!computerNameNew) {
error = ERROR_OUTOFMEMORY;
break;
}
computerName = computerNameNew;
}
while (1);
if (pcName)
{
wprintf("Computer name: %s\n", computerName);
}
else
{
wprintf(L"Error getting the name. Code: %ul\n", error);
}
free(computerName);
return 0;
}
Problem
I am getting `More data is available` error with the `GetComputerNameEx` function, but no idea how to fix it. This is my code: ``` int wmain() { COMPUTER_NAME_FORMAT nameType = ComputerNameDnsFullyQualified; WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD size = ARRAYSIZE(computerName); BOOL pcName = GetComputerNameEx(nameType, computerName, &size); DWORD error = GetLastError(); if (pcName != 0) { wprintf("Computer name: %s\n", computerName); } else { wprintf(L"Error getting the name. Code: %li\n", error); } return 0; } ``` No idea how to set `size` variable as output so I can declare the `computerName` array correctly.