ReactOS 0.4.15-dev-5666-gc548b97
gethostname.c File Reference
#include "ws2_32.h"
#include <winreg.h>
Include dependency graph for gethostname.c:

Go to the source code of this file.

Macros

#define REG_HOSTNAME_KEY   L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
 
#define REG_COMPUTERNAME_KEY   L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName"
 

Functions

HKEY OpenRegKey (IN HKEY hRootKey, IN PCWSTR pszSubKey, IN DWORD ulOptions)
 
BOOL GetHostnameFromCommand (OUT PSTR pszHostnameBuffer, IN ULONG ulBufferSize)
 
 START_TEST (gethostname)
 

Macro Definition Documentation

◆ REG_COMPUTERNAME_KEY

#define REG_COMPUTERNAME_KEY   L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName"

Definition at line 14 of file gethostname.c.

◆ REG_HOSTNAME_KEY

#define REG_HOSTNAME_KEY   L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters"

Definition at line 13 of file gethostname.c.

Function Documentation

◆ GetHostnameFromCommand()

BOOL GetHostnameFromCommand ( OUT PSTR  pszHostnameBuffer,
IN ULONG  ulBufferSize 
)

Definition at line 30 of file gethostname.c.

33{
34 PCSTR file_name = "HostName123.txt";
35 CHAR cmdline[MAX_PATH] = "hostname > ";
36 FILE *fp;
37 INT iResult;
38 INT len;
39 PCSTR ptr;
40
41 ZeroMemory(pszHostnameBuffer, ulBufferSize * sizeof(CHAR));
42
43 /* Run the 'hostname' command, piping out its result to the temporary file */
46
47 /* Open the temporary file in read mode */
48 fp = fopen(file_name, "r");
49 ok(fp != NULL, "An error occurred while opening the file.\n");
50 if (fp == NULL)
51 return FALSE;
52
53 /* Read its contents */
54 ptr = fgets(pszHostnameBuffer, ulBufferSize * sizeof(CHAR), fp);
55 ok(ptr != NULL, "An error occurred while reading the file.\n");
56 if (ptr == NULL)
57 goto Cleanup;
58
59 /* Delete the expected ending line feed character */
60 len = lstrlenA(pszHostnameBuffer);
61 if (pszHostnameBuffer[len-1] == '\r' || pszHostnameBuffer[len-1] == '\n')
62 pszHostnameBuffer[len-1] = ANSI_NULL;
63
65 /* Close and remove the file */
66 iResult = fclose(fp);
67 ok(iResult == 0, "An error occurred while closing the file: %i.\n", iResult);
68 iResult = remove(file_name);
69 ok(iResult == 0, "An error occurred while deleting the file: %i.\n", iResult);
70
71 return (ptr ? TRUE : FALSE);
72}
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define MAX_PATH
Definition: compat.h:34
static const WCHAR Cleanup[]
Definition: register.c:80
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
static PVOID ptr
Definition: dispmode.c:27
static LPCWSTR file_name
Definition: protocol.c:147
int remove
Definition: msacm.c:1366
#define ANSI_NULL
int __cdecl system(_In_opt_z_ const char *_Command)
TCHAR * cmdline
Definition: stretchblt.cpp:32
int32_t INT
Definition: typedefs.h:58
const char * PCSTR
Definition: typedefs.h:52
#define ZeroMemory
Definition: winbase.h:1670
char CHAR
Definition: xmlstorage.h:175

Referenced by START_TEST().

◆ OpenRegKey()

HKEY OpenRegKey ( IN HKEY  hRootKey,
IN PCWSTR  pszSubKey,
IN DWORD  ulOptions 
)

Definition at line 17 of file gethostname.c.

21{
22 HKEY hKey = NULL;
23 LONG Error = RegOpenKeyExW(hRootKey, pszSubKey, 0, ulOptions, &hKey);
24 if (Error == ERROR_SUCCESS)
25 return hKey;
26 return NULL;
27}
BOOL Error
Definition: chkdsk.c:66
#define ERROR_SUCCESS
Definition: deptool.c:10
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3356
FxAutoRegKey hKey
long LONG
Definition: pedump.c:60

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( gethostname  )

Definition at line 74 of file gethostname.c.

75{
76 INT pos;
77 HKEY hKeyHN;
78 DWORD cbData;
79 LONG Error;
80 INT hnError;
81 INT iResult;
82 WSADATA wsaData;
83 DWORD uApiHostNameSize;
84 CHAR szApiHostName[256] = "";
85 CHAR szHostNameOld[256] = "";
86 CHAR szHostNameNew[256] = "";
87 CHAR hostbuffer[256] = "";
88
89/*
90 * Notes about the retrieval of the computer name on Windows.
91 *
92 * - GetComputerName() (equivalently, GetComputerNameEx(ComputerNameNetBIOS))
93 * retrieves the name cached in the registry value "ComputerName" under
94 * "System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName" .
95 *
96 * - GetComputerNameEx(ComputerNameDnsHostname) as well as the 'hostname' command
97 return the registry value "Hostname" under "System\\CurrentControlSet\\Services\\Tcpip\\Parameters" .
98 *
99 * - In case a new computer name is set, its value is cached in the registry value
100 * "ComputerName" under "System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
101 * and also in the registry value "NV Hostname" under "System\\CurrentControlSet\\Services\\Tcpip\\Parameters" .
102 */
103
104 /* Retrieve the current host name using API */
105 uApiHostNameSize = _countof(szApiHostName);
106 if (!GetComputerNameExA(ComputerNameDnsHostname, szApiHostName, &uApiHostNameSize))
107 {
108 /* Fail in case of error */
109 skip("GetComputerNameExA(ComputerNameDnsHostname) failed, error %lu\n", GetLastError());
110 return;
111 }
112 printf("The Hostname from API is '%s'.\n", szApiHostName);
113
114 /* Retrieve the current host name using the 'hostname' command */
115 if (!GetHostnameFromCommand(hostbuffer, _countof(hostbuffer)))
116 {
117 /* Fail in case of error */
118 skip("Error while retrieving the host name using the 'hostname' command!\n");
119 return;
120 }
121 printf("The Hostname from command is '%s'.\n", hostbuffer);
122
123 pos = strcmp(szApiHostName, hostbuffer);
124 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
125 ok(pos == 0, "hostbuffer '%s' should have been szApiHostName '%s'.\n", hostbuffer, szApiHostName);
126
128 ok(hKeyHN != NULL, "Error while opening hostname registry key.\n");
129 if (hKeyHN == NULL)
130 return;
131
132 /* Get Old Hostname */
133 szHostNameOld[0] = ANSI_NULL;
134 cbData = sizeof(szHostNameOld);
135 Error = RegQueryValueExA(hKeyHN, "Hostname", NULL, NULL, (LPBYTE)szHostNameOld, &cbData);
136
137 printf("Hostname from Registry is '%s'.\n", szHostNameOld);
138
139 pos = strcmp(szHostNameOld, szApiHostName);
140 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
141 ok(pos == 0, "szApiHostName '%s' should have been szHostNameOld '%s'.\n", szApiHostName, szHostNameOld);
142
143 /* Change Hostname in the Registry */
144 szHostNameNew[0] = ANSI_NULL;
145 strcat(szHostNameNew, "ROSHOSTNAME1");
146 cbData = lstrlenA(szHostNameNew) + 1;
147
148 Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameNew, cbData);
149 ok(Error == ERROR_SUCCESS, "Error setting new registry value (%ld).\n", GetLastError());
150
151 /* Retrieve the current host name using API */
152 uApiHostNameSize = _countof(szApiHostName);
153 if (!GetComputerNameExA(ComputerNameDnsHostname, szApiHostName, &uApiHostNameSize))
154 {
155 /* Fail in case of error */
156 skip("GetComputerNameExA(ComputerNameDnsHostname) failed, error %lu\n", GetLastError());
157 goto Cleanup;
158 }
159 printf("The Hostname from API is '%s'.\n", szApiHostName);
160
161 /* Retrieve the current host name using the 'hostname' command */
162 if (!GetHostnameFromCommand(hostbuffer, _countof(hostbuffer)))
163 {
164 /* Fail in case of error */
165 skip("Error while retrieving the host name using the 'hostname' command!\n");
166 goto Cleanup;
167 }
168 printf("The Hostname from command is '%s'.\n", hostbuffer);
169
170 pos = strcmp(szHostNameNew, szApiHostName);
171 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
172 ok(pos == 0, "szApiHostName '%s' should be szHostNameNew '%s'.\n", szApiHostName, szHostNameNew);
173
174 pos = strcmp(szHostNameNew, hostbuffer);
175 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
176 ok(pos == 0, "hostbuffer '%s' should have been szHostNameNew '%s'.\n", hostbuffer, szHostNameNew);
177
178 /* Reset the original registry entry */
179 cbData = lstrlenA(szHostNameOld) + 1;
180
181 Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameOld, cbData);
182 ok(Error == ERROR_SUCCESS, "Error resetting new registry value back (%ld).\n", GetLastError());
183
184/*============ Winsock Checks ===============*/
185
186 /* Start up Winsock to use gethostname() */
187 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
188 ok(iResult == 0, "Error occurred starting Winsock");
189 if (iResult != 0)
190 goto Cleanup;
191
192 /* Retrieve gethostname() */
193 hnError = gethostname(hostbuffer, sizeof(hostbuffer));
194 ok(!hnError, "Winsock gethostname() Error is '%d'.\n", WSAGetLastError());
195
196 /* Display results */
197 if (!hnError)
198 printf("Winsock gethostname() is '%s'.\n", hostbuffer);
199
200 pos = strcmp(szHostNameOld, hostbuffer);
201 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
202 ok(pos == 0, "szHostNameOld '%s' should be hostbuffer '%s'.\n", szHostNameOld, hostbuffer);
203
204 /* Change Hostname in the Registry */
205 szHostNameNew[0] = ANSI_NULL;
206 strcat(szHostNameNew, "ROSHOSTNAME1");
207 cbData = lstrlenA(szHostNameNew) + 1;
208
209 Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameNew, cbData);
210 ok(Error == ERROR_SUCCESS, "Error setting new registry value (%ld).\n", GetLastError());
211
212 /* Retrieve gethostname() */
213 hnError = gethostname(hostbuffer, sizeof(hostbuffer));
214 ok(!hnError, "Winsock gethostname() Error is '%d'.\n", WSAGetLastError());
215
216 /* Display results */
217 if (!hnError)
218 printf("Winsock gethostname() is '%s'.\n", hostbuffer);
219
220 pos = strcmp(szHostNameNew, hostbuffer);
221 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
222 ok(pos == 0, "szHostNameNew '%s' should be hostbuffer '%s'.\n", szHostNameNew, hostbuffer);
223
224 /* Reset the original registry entry */
225 cbData = lstrlenA(szHostNameOld) + 1;
226
227 Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameOld, cbData);
228 ok(Error == ERROR_SUCCESS, "Error resetting new registry value back (%ld).\n", GetLastError());
229
230 /* Retrieve gethostname() */
231 hnError = gethostname(hostbuffer, sizeof(hostbuffer));
232 ok(!hnError, "Winsock gethostname() Error is '%d'.\n", WSAGetLastError());
233
234 /* Display results */
235 if (!hnError)
236 printf("Winsock gethostname() is '%s'.\n", hostbuffer);
237
238 pos = strcmp(szHostNameOld, hostbuffer);
239 printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
240 ok(pos == 0, "szHostNameOld '%s' should be hostbuffer '%s'.\n", szHostNameOld, hostbuffer);
241
242Cleanup:
243 iResult = WSACleanup();
244 ok(iResult == 0, "WSACleanup error occurred ending Winsock");
245
246 Error = RegCloseKey(hKeyHN);
247 ok(Error == ERROR_SUCCESS, "Error closing registry key (%ld).\n", GetLastError());
248
249 return;
250}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define skip(...)
Definition: atltest.h:64
#define RegCloseKey(hKey)
Definition: registry.h:47
LONG WINAPI RegSetValueExA(HKEY hKey, LPCSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE *lpData, DWORD cbData)
Definition: reg.c:4817
LONG WINAPI RegQueryValueExA(_In_ HKEY hkeyorg, _In_ LPCSTR name, _In_ LPDWORD reserved, _Out_opt_ LPDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ LPDWORD count)
Definition: reg.c:4027
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
unsigned long DWORD
Definition: ntddk_ex.h:95
#define printf
Definition: freeldr.h:94
BOOL GetHostnameFromCommand(OUT PSTR pszHostnameBuffer, IN ULONG ulBufferSize)
Definition: gethostname.c:30
HKEY OpenRegKey(IN HKEY hRootKey, IN PCWSTR pszSubKey, IN DWORD ulOptions)
Definition: gethostname.c:17
#define REG_HOSTNAME_KEY
Definition: gethostname.c:13
INT WSAAPI gethostname(OUT char FAR *name, IN INT namelen)
Definition: getxbyxx.c:397
BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT, LPSTR, LPDWORD)
Definition: compname.c:376
#define REG_SZ
Definition: layer.c:22
#define KEY_ALL_ACCESS
Definition: nt_native.h:1041
#define _countof(array)
Definition: sndvol32.h:68
#define MAKEWORD(a, b)
Definition: typedefs.h:248
unsigned char * LPBYTE
Definition: typedefs.h:53
DWORD WINAPI GetLastError(void)
Definition: except.c:1040
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:112
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60