GetComputerName fails on Windows with max length computer name


#1
String SystemStats::getComputerName()
{
    TCHAR text [MAX_COMPUTERNAME_LENGTH + 1] = { 0 };
    DWORD len = (DWORD) numElementsInArray (text) - 1;
    GetComputerName (text, &len);
    return String (text, len);
}

That -1 shouldn't be there. 

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.

On input it should be buffer size, on output it is number of characters.


#2

Thanks, will get that fixed!


#3

Hi,
Modern Windows OS allow computer name with more than 15 characteres,

So I propose to update SystemStats::getComputerName() in juce_win32_SystemStats.cpp of Juce 5.4.1, by replacing GetComputerName with GetComputerNameEx like this:

String SystemStats::getComputerName()
{
    TCHAR text[128] = { 0 };
    DWORD len = (DWORD) numElementsInArray (text) - 1;
    GetComputerNameEx(ComputerNamePhysicalDnsHostname, text, &len);
    return String (text, len);
}

This fix the truncated computer name issue.
Hopping this can helps.
All the best.


#4

Looking at the docs for GetComputerName() and GetComputerNameEx() it looks like we should be using ComputerNameNetBIOS for the NameType argument for backwards compatibility - GetComputerName() says that it retrieves the “computer name or the cluster virtual server name” which is the same as ComputerNameNetBIOS whereas ComputerNamePhysicalDnsHostname says:

The DNS host name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS host name of the local computer, not the name of the cluster virtual server.

What do you think? Is there a particular reason that you have used ComputerNamePhysicalDnsHostname?


#5

Using ComputerNameNetBIOS and ComputerNamePhysicalNetBIOS with GetComputerNameEx() return the 15 charactere max cropped version ( like GetComputerName old behaviour).

I promote “PhysicalNameType to be local and avoid GetComputerNameEx perform slow network query that could timeout when domain is not reachable.

That is why I choose ComputerNamePhysicalDnsHostname

Hopping all of this can help.


#6

Thanks for the clarification. I’ve added this to the develop branch here.


#7

Perfect many thanks for that
All the best.