2 * PROJECT: ReactOS Hostname Command
3 * LICENSE: LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4 * PURPOSE: Retrieves the current DNS host name of the computer.
5 * COPYRIGHT: Copyright 2005-2019 Emanuele Aliberti (ea@reactos.com)
6 * Copyright 2019 Hermes Belusca-Maito
18 int wmain(int argc
, WCHAR
* argv
[])
25 WCHAR LocalHostName
[256] = L
""; // MAX_COMPUTERNAME_LENGTH + 1 for NetBIOS name.
26 DWORD HostNameSize
= _countof(LocalHostName
);
27 PWSTR HostName
= LocalHostName
;
29 /* Try to retrieve the host name using the local buffer */
30 bSuccess
= GetComputerNameExW(ComputerNameDnsHostname
, HostName
, &HostNameSize
);
31 if (!bSuccess
&& (GetLastError() == ERROR_MORE_DATA
))
33 /* Retry with a larger buffer since the local buffer was too small */
34 HostName
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HostNameSize
* sizeof(WCHAR
));
36 bSuccess
= GetComputerNameExW(ComputerNameDnsHostname
, HostName
, &HostNameSize
);
41 /* Print out the host name */
42 _cwprintf(L
"%s\n", HostName
);
45 /* If a larger buffer has been allocated, free it */
46 if (HostName
&& (HostName
!= LocalHostName
))
47 HeapFree(GetProcessHeap(), 0, HostName
);
51 /* Fail in case of error */
52 LoadStringW(GetModuleHandle(NULL
), IDS_ERROR
, Msg
, _countof(Msg
));
53 _cwprintf(L
"%s %lu.\n", Msg
, GetLastError());
59 if ((wcsicmp(argv
[1], L
"-s") == 0) || (wcsicmp(argv
[1], L
"/s") == 0))
61 /* The program doesn't allow the user to set the host name */
62 LoadStringW(GetModuleHandle(NULL
), IDS_NOSET
, Msg
, _countof(Msg
));
63 _cwprintf(L
"%s\n", Msg
);
68 /* Let the user know what the program does */
69 LoadStringW(GetModuleHandle(NULL
), IDS_USAGE
, Msg
, _countof(Msg
));
70 _cwprintf(L
"\n%s\n\n", Msg
);