0

I'm trying to make a function that returns the name of the computer. This is what I have so far:

char* getName()
{
    char buffer[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD length = sizeof(buffer);

    GetComputerNameEx((COMPUTER_NAME_FORMAT)0, buffer, &length);

    return buffer;
}

But it doesn't really work. It seems it returns a string with very strange character encoding. I would appreciate all help.

Shog9
  • 156,901
  • 35
  • 231
  • 235
hsson
  • 595
  • 1
  • 7
  • 16

1 Answers1

5

Use an std::string to avoid memory issues. And as std::string uses single byte characters, you should use the single-byte version of GetComputerNameEx, GetComputerNameExA.

Also it is a good idea to check the error value, as the documentation explicitly states that the computer name might be longer than MAX_COMPUTERNAME_LENGTH.

std::string getName()
{
    char buffer[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD length = sizeof(buffer);

    bool ok = GetComputerNameExA((COMPUTER_NAME_FORMAT)0, buffer, &length);

    if (ok) { 
        return buffer;
    } else {
        //handle error
    }
}

Or you can use the wide version:

std::wstring getName()
{
    wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD length = sizeof(buffer) / sizeof(wchar_t);

    bool ok = GetComputerNameExW((COMPUTER_NAME_FORMAT)0, buffer, &length);

    if (ok) { 
        return buffer;
    } else {
        //handle error
    }
}

or make a code that works under both environments (idea from here):

std::basic_string<TCHAR> getName()
{
    TCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD length = sizeof(buffer) / sizeof(TCHAR);

    bool ok = GetComputerNameEx((COMPUTER_NAME_FORMAT)0, buffer, &length);

    if (ok) { 
        return buffer;
    } else {
        //handle error
    }
}
Community
  • 1
  • 1
Csq
  • 5,775
  • 6
  • 26
  • 39
  • +1 for mentioning use of TCHAR. Aside from other errors, the question mentions strange encoding of the returned string, which is most likely to do with building in Unicode. – icabod Jun 09 '14 at 11:15
  • Please don't advise the use of `TCHAR` today. That was useful when you needed code to compile under both Win95 and WinNT. No longer is this useful. – David Heffernan Jun 09 '14 at 11:16
  • @DavidHeffernan: That's true, especially with the gradual deprecation of MCBS builds in Visual Studio, but using `TCHAR` at least would match the rest of the build. If using VS. – icabod Jun 09 '14 at 11:19
  • @icabod The right way to do it is to compile for `UNICODE` and use `wchar_t`, `std::wstring`, `L"..."` etc. Using `TCHAR` just adds a useless and confusing level of indirection. – David Heffernan Jun 09 '14 at 11:22
  • In either case, you should pass the final `length` to the string constructor and not rely on the `buffer` being null-terminated, eg: `return std::string(buffer, length);` or `return std::wstring(buffer, length);` or `return std::basic_string(buffer, length);` – Remy Lebeau Jun 09 '14 at 20:27
  • @RemyLebeau the returned value seems to be null-terminated. Is there any explicit reason for this? (I've read the MSDN page again and what seems to be is even more important is to check the return value to be true) – Csq Jun 09 '14 at 22:43