[HOSTNAME] Retrieve the DNS *host name* of the computer, and not the computer name.

CORE-16095, ROSTESTS-326
This commit is contained in:
Hermès Bélusca-Maïto 2019-06-16 01:00:40 +02:00
parent 077a1b8dba
commit 28ed2347ef
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -1,28 +1,12 @@
/*
* ReactOS Win32 Applications
* Copyright (C) 2005 ReactOS Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* COPYRIGHT : See COPYING in the top level directory
* PROJECT : ReactOS/Win32 get host name
* FILE : subsys/system/hostname/hostname.c
* PROGRAMMER: Emanuele Aliberti (ea@reactos.com)
* PROJECT: ReactOS Hostname Command
* LICENSE: LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
* PURPOSE: Retrieves the current DNS host name of the computer.
* COPYRIGHT: Copyright 2005-2019 Emanuele Aliberti (ea@reactos.com)
* Copyright 2019 Hermes Belusca-Maito
*/
#include <stdlib.h>
#include <conio.h>
#include <windef.h>
@ -35,35 +19,54 @@ int wmain(int argc, WCHAR* argv[])
{
WCHAR Msg[100];
if (1 == argc)
if (argc == 1)
{
WCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1] = L"";
DWORD ComputerNameSize = sizeof(ComputerName) / sizeof(ComputerName[0]);
BOOL bSuccess;
WCHAR LocalHostName[256] = L""; // MAX_COMPUTERNAME_LENGTH + 1 for NetBIOS name.
DWORD HostNameSize = _countof(LocalHostName);
PWSTR HostName = LocalHostName;
if (!GetComputerName(ComputerName, &ComputerNameSize))
/* Try to retrieve the host name using the local buffer */
bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
if (!bSuccess && (GetLastError() == ERROR_MORE_DATA))
{
/* Retry with a larger buffer since the local buffer was too small */
HostName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HostNameSize * sizeof(WCHAR));
if (HostName)
bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
}
if (bSuccess)
{
/* Print out the host name */
_cwprintf(L"%s\n", HostName);
}
/* If a larger buffer has been allocated, free it */
if (HostName && (HostName != LocalHostName))
HeapFree(GetProcessHeap(), 0, HostName);
if (!bSuccess)
{
/* Fail in case of error */
LoadStringW(GetModuleHandle(NULL), IDS_ERROR, Msg, 100);
LoadStringW(GetModuleHandle(NULL), IDS_ERROR, Msg, _countof(Msg));
_cwprintf(L"%s %lu.\n", Msg, GetLastError());
return 1;
}
/* Print out the computer's name */
_cwprintf(L"%s\n", ComputerName);
}
else
{
if ((wcsicmp(argv[1], L"-s") == 0) || (wcsicmp(argv[1], L"/s") == 0))
{
/* The program doesn't allow the user to set the computer's name */
LoadStringW(GetModuleHandle(NULL), IDS_NOSET, Msg, 100);
/* The program doesn't allow the user to set the host name */
LoadStringW(GetModuleHandle(NULL), IDS_NOSET, Msg, _countof(Msg));
_cwprintf(L"%s\n", Msg);
return 1;
}
else
{
/* Let the user know what the program does */
LoadStringW(GetModuleHandle(NULL), IDS_USAGE, Msg, 100);
LoadStringW(GetModuleHandle(NULL), IDS_USAGE, Msg, _countof(Msg));
_cwprintf(L"\n%s\n\n", Msg);
}
}