3 * Copyright (C) 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * PURPOSE: Computer name functions
24 * FILE: lib/kernel32/misc/computername.c
25 * PROGRAMER: Eric Kohl (ekohl@rz-online.de)
28 /* INCLUDES ******************************************************************/
33 #include <kernel32/kernel32.h>
36 /* FUNCTIONS *****************************************************************/
42 GetComputerNameA (LPSTR lpBuffer,
45 UNICODE_STRING UnicodeString;
46 ANSI_STRING AnsiString;
48 AnsiString.MaximumLength = *lpnSize;
49 AnsiString.Length = 0;
50 AnsiString.Buffer = lpBuffer;
52 UnicodeString.MaximumLength = *lpnSize * sizeof(WCHAR);
53 UnicodeString.Length = 0;
54 UnicodeString.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
56 UnicodeString.MaximumLength);
57 if (UnicodeString.Buffer == NULL)
59 SetLastError (ERROR_OUTOFMEMORY);
63 if (!GetComputerNameW (UnicodeString.Buffer, lpnSize))
65 RtlFreeUnicodeString (&UnicodeString);
69 UnicodeString.Length = *lpnSize * sizeof(WCHAR);
71 RtlUnicodeStringToAnsiString (&AnsiString,
75 RtlFreeUnicodeString (&UnicodeString);
85 GetComputerNameW (LPWSTR lpBuffer,
88 PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
89 OBJECT_ATTRIBUTES ObjectAttributes;
90 UNICODE_STRING KeyName;
91 UNICODE_STRING ValueName;
97 RtlInitUnicodeString (&KeyName,
98 L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName");
99 InitializeObjectAttributes (&ObjectAttributes,
101 OBJ_CASE_INSENSITIVE,
104 Status = NtOpenKey (&KeyHandle,
107 if (!NT_SUCCESS(Status))
109 SetLastErrorByStatus (Status);
113 KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) +
114 *lpnSize * sizeof(WCHAR);
115 KeyInfo = RtlAllocateHeap (RtlGetProcessHeap (),
121 SetLastError (ERROR_OUTOFMEMORY);
125 RtlInitUnicodeString (&ValueName,
128 Status = NtQueryValueKey (KeyHandle,
130 KeyValuePartialInformation,
134 if (!NT_SUCCESS(Status))
136 RtlFreeHeap (RtlGetProcessHeap (),
140 SetLastErrorByStatus (Status);
145 (KeyInfo->DataLength != 0) ? (KeyInfo->DataLength / sizeof(WCHAR)) - 1 : 0;
147 RtlCopyMemory (lpBuffer,
149 KeyInfo->DataLength);
150 lpBuffer[*lpnSize] = 0;
152 RtlFreeHeap (RtlGetProcessHeap (),
165 SetComputerNameA (LPCSTR lpComputerName)
167 UNICODE_STRING ComputerName;
170 RtlCreateUnicodeStringFromAsciiz (&ComputerName,
171 (LPSTR)lpComputerName);
173 bResult = SetComputerNameW (ComputerName.Buffer);
175 RtlFreeUnicodeString (&ComputerName);
185 IsValidComputerName (LPCWSTR lpComputerName)
191 p = (PWCHAR)lpComputerName;
194 if (!(iswctype (*p, _ALPHA || _DIGIT) ||
218 Length > MAX_COMPUTERNAME_LENGTH)
229 SetComputerNameW (LPCWSTR lpComputerName)
231 OBJECT_ATTRIBUTES ObjectAttributes;
232 UNICODE_STRING KeyName;
233 UNICODE_STRING ValueName;
237 if (!IsValidComputerName (lpComputerName))
239 SetLastError (ERROR_INVALID_PARAMETER);
243 RtlInitUnicodeString (&KeyName,
244 L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName");
245 InitializeObjectAttributes (&ObjectAttributes,
247 OBJ_CASE_INSENSITIVE,
250 Status = NtOpenKey (&KeyHandle,
253 if (!NT_SUCCESS(Status))
255 SetLastErrorByStatus (Status);
259 RtlInitUnicodeString (&ValueName,
262 Status = NtSetValueKey (KeyHandle,
266 (PVOID)lpComputerName,
267 (wcslen (lpComputerName) + 1) * sizeof(WCHAR));
268 if (!NT_SUCCESS(Status))
271 SetLastErrorByStatus (Status);
275 NtFlushKey (KeyHandle);