Reputation: 1044
I've been searching around the internet about this issue and it seems really strange to me. In my program i am getting the name of the computer by the following code:
TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
memset( ComputerName, 0, sizeof( ComputerName ) );
DWORD dwNameLenght = MAX_COMPUTERNAME_LENGTH;
GetComputerNameW(ComputerName, &dwNameLenght);
I've tested it with a computer name(ÆÆØ
) that contains unicode characters and it works fine(although GetLastError()
returns code of 203).
If the name is DESKTOP-IVHSIQD
no computer name is obtained and GetLastError()
returns 111, which is said to be buffer overflow.
The operating system is Windows 10 x64.
Upvotes: 2
Views: 2008
Reputation: 29966
I think your buffer is indeed too small... The documentation on GetComputerNameW
states:
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. The lpnSize parameter specifies the size of the buffer required, including the terminating null character.
To me that implies that both the buffer itself, and its size in lpnSize
should be enough to hold the longest possible computer name and the trailing \0
.
Your buffer is of MAX_COMPUTERNAME_LENGTH + 1
size (which is fine according to the documentation), but the value of dwNameLenght
is 1 character less. DESKTOP-IVHSIQD
also seems to be exactly MAX_COMPUTERNAME_LENGTH
long (15 characters), so I would say the overflow happens when the \0
is being appended to the buffer. Setting dwNameLenght
to MAX_COMPUTERNAME_LENGTH + 1
might solve your problem.
Upvotes: 5