On Thursday, 26 March 2015 at 16:46:06 UTC, Belly wrote:
No, wait, the first code is even better because it uses the headers so I don't need to declare the API myself and it uses the MAX_COMPUTERNAME_LENGTH define, too, nice!

Using this will return only the first 15 characters of the computer name, if longer.

Please use the unicode version of the function GetComputerNameW instead of the ANSI one (GetComputerNameA), because the second one has a known bug, returning always 0 as required buffer size.

import std.stdio;
import std.c.windows.windows;

wstring getComputerName()
{
        enum ERROR_BUFFER_OVERFLOW = 111;
        uint size;
if (GetComputerNameW(null, &size) == 0 && GetLastError() == ERROR_BUFFER_OVERFLOW)
        {
                wchar[] buf = new wchar[size];
                if (GetComputerNameW(buf.ptr, &size))
                        return buf[0 .. size].idup;
        }
        return "Unknown";
}

int main(string[] argv)
{
        writeln(getComputerName());
        getchar();
    return 0;
}









Reply via email to