ReactOS  0.4.15-dev-2144-g2282205
hostname.c File Reference
#include <stdlib.h>
#include <conio.h>
#include <windef.h>
#include <winbase.h>
#include <winuser.h>
#include "resource.h"
Include dependency graph for hostname.c:

Go to the source code of this file.

Functions

int wmain (int argc, WCHAR *argv[])
 

Function Documentation

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

Definition at line 18 of file hostname.c.

19 {
20  WCHAR Msg[100];
21 
22  if (argc == 1)
23  {
24  BOOL bSuccess;
25  WCHAR LocalHostName[256] = L""; // MAX_COMPUTERNAME_LENGTH + 1 for NetBIOS name.
26  DWORD HostNameSize = _countof(LocalHostName);
27  PWSTR HostName = LocalHostName;
28 
29  /* Try to retrieve the host name using the local buffer */
30  bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
31  if (!bSuccess && (GetLastError() == ERROR_MORE_DATA))
32  {
33  /* Retry with a larger buffer since the local buffer was too small */
34  HostName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HostNameSize * sizeof(WCHAR));
35  if (HostName)
36  bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
37  }
38 
39  if (bSuccess)
40  {
41  /* Print out the host name */
42  _cwprintf(L"%s\n", HostName);
43  }
44 
45  /* If a larger buffer has been allocated, free it */
46  if (HostName && (HostName != LocalHostName))
47  HeapFree(GetProcessHeap(), 0, HostName);
48 
49  if (!bSuccess)
50  {
51  /* Fail in case of error */
53  _cwprintf(L"%s %lu.\n", Msg, GetLastError());
54  return 1;
55  }
56  }
57  else
58  {
59  if ((wcsicmp(argv[1], L"-s") == 0) || (wcsicmp(argv[1], L"/s") == 0))
60  {
61  /* The program doesn't allow the user to set the host name */
63  _cwprintf(L"%s\n", Msg);
64  return 1;
65  }
66  else
67  {
68  /* Let the user know what the program does */
70  _cwprintf(L"\n%s\n\n", Msg);
71  }
72  }
73 
74  return 0;
75 }
static int argc
Definition: ServiceArgs.c:12
uint16_t * PWSTR
Definition: typedefs.h:56
struct @1616 Msg[]
int __cdecl _cwprintf(const MSVCRT_wchar_t *,...)
Definition: _cwprintf.c:14
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
#define IDS_USAGE
Definition: resource.h:3
#define argv
Definition: mplay32.c:18
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
static BOOLEAN bSuccess
Definition: drive.cpp:462
BOOL WINAPI GetComputerNameExW(COMPUTER_NAME_FORMAT NameType, LPWSTR lpBuffer, LPDWORD nSize)
Definition: compname.c:216
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IDS_NOSET
Definition: resource.h:4
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define _countof(array)
Definition: sndvol32.h:68
unsigned long DWORD
Definition: ntddk_ex.h:95
#define wcsicmp
Definition: compat.h:15
static const WCHAR L[]
Definition: oid.c:1250
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define GetModuleHandle
Definition: winbase.h:3683
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define IDS_ERROR
Definition: resource.h:18
#define HeapFree(x, y, z)
Definition: compat.h:594