ReactOS 0.4.16-dev-2534-gdcd45a5
hostname.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <windef.h>
#include <winbase.h>
#include <conutils.h>
#include "resource.h"
Include dependency graph for hostname.c:

Go to the source code of this file.

Functions

static VOID PrintError (_In_opt_ PCWSTR Message, _In_ DWORD dwError)
 
int wmain (int argc, WCHAR *argv[])
 

Function Documentation

◆ PrintError()

static VOID PrintError ( _In_opt_ PCWSTR  Message,
_In_ DWORD  dwError 
)
static

Definition at line 20 of file hostname.c.

23{
24 INT Len;
25
26 if (dwError == ERROR_SUCCESS)
27 return;
28
31 else // if (Message)
34 NULL, dwError, LANG_USER_DEFAULT);
35 if (Len <= 0) /* Fall back in case the error is not defined */
36 ConPrintf(StdErr, L"%lu\n", dwError);
37}
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: conutils_noros.h:8
void ConPrintf(FILE *fp, LPCWSTR psz,...)
#define StdErr
Definition: conutils_noros.h:7
void ConResPuts(FILE *fp, UINT nID)
#define Len
Definition: deflate.h:82
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define IS_INTRESOURCE(x)
Definition: loader.c:613
static const WCHAR Message[]
Definition: register.c:74
#define L(x)
Definition: resources.c:13
#define PtrToUlong(u)
Definition: config.h:107
INT ConMsgPuts(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId)
Definition: outstream.c:835
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
int32_t INT
Definition: typedefs.h:58
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:400

Referenced by wmain().

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

Definition at line 39 of file hostname.c.

40{
41 /* Initialize the Console Standard Streams */
43
44 if (argc == 1)
45 {
46 BOOL bSuccess;
47 WCHAR LocalHostName[256] = L""; // MAX_COMPUTERNAME_LENGTH + 1 for NetBIOS name.
48 DWORD HostNameSize = _countof(LocalHostName);
49 PWSTR HostName = LocalHostName;
50
51 /* Try to retrieve the host name using the local buffer */
52 bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
53 if (!bSuccess && (GetLastError() == ERROR_MORE_DATA))
54 {
55 /* Retry with a larger buffer since the local buffer was too small */
56 HostName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HostNameSize * sizeof(WCHAR));
57 if (HostName)
58 bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
59 }
60
61 /* Print out the host name */
62 if (bSuccess)
63 ConPrintf(StdOut, L"%s\n", HostName);
64
65 /* If a larger buffer has been allocated, free it */
66 if (HostName && (HostName != LocalHostName))
67 HeapFree(GetProcessHeap(), 0, HostName);
68
69 if (!bSuccess)
70 {
71 /* Fail in case of error */
73 return 1;
74 }
75 }
76 else
77 {
78 if ((_wcsicmp(argv[1], L"-s") == 0) || (_wcsicmp(argv[1], L"/s") == 0))
79 {
80 /* The program doesn't allow the user to set the host name */
82 return 1;
83 }
84 else
85 {
86 /* Let the user know what the program does */
88 }
89 }
90
91 return 0;
92}
#define IDS_USAGE
Definition: resource.h:3
#define IDS_ERROR
Definition: resource.h:18
#define IDS_NOSET
Definition: resource.h:4
BOOL WINAPI GetComputerNameExW(COMPUTER_NAME_FORMAT NameType, LPWSTR lpBuffer, LPDWORD nSize)
Definition: compname.c:216
#define ConInitStdStreams()
Definition: conutils_noros.h:5
#define StdOut
Definition: conutils_noros.h:6
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
MonoAssembly int argc
Definition: metahost.c:107
_ACRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:159
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
static VOID PrintError(_In_opt_ PCWSTR Message, _In_ DWORD dwError)
Definition: hostname.c:20
#define argv
Definition: mplay32.c:18
#define _countof(array)
Definition: sndvol32.h:70
uint16_t * PWSTR
Definition: typedefs.h:56
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
__wchar_t WCHAR
Definition: xmlstorage.h:180