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 "../include/debug.h"
36 /* FUNCTIONS *****************************************************************/
38 static BOOL
GetComputerNameFromRegistry( LPWSTR RegistryKey
,
42 PKEY_VALUE_PARTIAL_INFORMATION KeyInfo
;
43 OBJECT_ATTRIBUTES ObjectAttributes
;
44 UNICODE_STRING KeyName
;
45 UNICODE_STRING ValueName
;
51 RtlInitUnicodeString (&KeyName
,RegistryKey
);
52 InitializeObjectAttributes (&ObjectAttributes
,
57 Status
= ZwOpenKey (&KeyHandle
,
60 if (!NT_SUCCESS(Status
))
62 SetLastErrorByStatus (Status
);
66 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) +
67 *nSize
* sizeof(WCHAR
);
68 KeyInfo
= RtlAllocateHeap (RtlGetProcessHeap (),
74 SetLastError (ERROR_OUTOFMEMORY
);
78 RtlInitUnicodeString (&ValueName
,ValueNameStr
);
80 Status
= ZwQueryValueKey (KeyHandle
,
82 KeyValuePartialInformation
,
86 if (!NT_SUCCESS(Status
))
88 RtlFreeHeap (RtlGetProcessHeap (),
92 SetLastErrorByStatus (Status
);
96 if( *nSize
> (KeyInfo
->DataLength
/ sizeof(WCHAR
)) ) {
97 *nSize
= KeyInfo
->DataLength
/ sizeof(WCHAR
);
101 RtlCopyMemory (lpBuffer
,
103 *nSize
* sizeof(WCHAR
));
105 RtlFreeHeap (RtlGetProcessHeap (),
119 COMPUTER_NAME_FORMAT NameType
,
124 UNICODE_STRING ResultString
;
125 UNICODE_STRING DomainPart
, Dot
;
126 UNICODE_STRING RegKey
, RegValue
;
129 case ComputerNameNetBIOS
:
130 return GetComputerNameFromRegistry
131 ( L
"\\Registry\\Machine\\System\\CurrentControlSet"
132 L
"\\Control\\ComputerName\\ComputerName",
137 case ComputerNameDnsDomain
:
138 return GetComputerNameFromRegistry
139 ( L
"\\Registry\\Machine\\System\\CurrentControlSet"
140 L
"\\Services\\Tcpip\\Parameters",
145 case ComputerNameDnsFullyQualified
:
146 RtlInitUnicodeString(&Dot
,L
".");
147 RosInitializeString(&ResultString
,0,*nSize
* sizeof(WCHAR
),lpBuffer
);
148 RtlInitUnicodeString(&RegKey
,
149 L
"\\Registry\\Machine\\System"
150 L
"\\CurrentControlSet\\Services\\Tcpip"
152 RtlInitUnicodeString(&RegValue
,L
"HostName");
153 RtlInitUnicodeString(&DomainPart
,L
"");
154 if( NT_SUCCESS(RosReadRegistryValue(&RegKey
,&RegValue
,&DomainPart
)) ) {
155 RtlAppendUnicodeStringToString(&ResultString
,&DomainPart
);
156 RtlAppendUnicodeStringToString(&ResultString
,&Dot
);
157 RtlFreeUnicodeString(&DomainPart
);
158 RtlInitUnicodeString(&RegValue
,L
"Domain");
159 RtlInitUnicodeString(&DomainPart
,L
"");
160 if( NT_SUCCESS(RosReadRegistryValue
161 (&RegKey
,&RegValue
,&DomainPart
)) ) {
162 RtlAppendUnicodeStringToString(&ResultString
,&DomainPart
);
163 RtlFreeUnicodeString(&DomainPart
);
164 *nSize
= ResultString
.Length
/ sizeof(WCHAR
);
170 case ComputerNameDnsHostname
:
171 return GetComputerNameFromRegistry
172 ( L
"\\Registry\\Machine\\System\\CurrentControlSet"
173 L
"\\Services\\Tcpip\\Parameters",
178 case ComputerNamePhysicalDnsDomain
:
179 return GetComputerNameFromRegistry
180 ( L
"\\Registry\\Machine\\System\\CurrentControlSet"
181 L
"\\Services\\Tcpip\\Parameters",
187 case ComputerNamePhysicalDnsFullyQualified
:
188 return GetComputerNameExW( ComputerNameDnsFullyQualified
,
190 case ComputerNamePhysicalDnsHostname
:
191 return GetComputerNameExW( ComputerNameDnsHostname
,
193 case ComputerNamePhysicalNetBIOS
:
194 return GetComputerNameExW( ComputerNameNetBIOS
,
197 case ComputerNameMax
:
210 COMPUTER_NAME_FORMAT NameType
,
215 UNICODE_STRING UnicodeString
;
216 ANSI_STRING AnsiString
;
218 PWCHAR TempBuffer
= RtlAllocateHeap( GetProcessHeap(), 0, *nSize
* sizeof(WCHAR
) );
221 return ERROR_OUTOFMEMORY
;
224 AnsiString
.MaximumLength
= *nSize
;
225 AnsiString
.Length
= 0;
226 AnsiString
.Buffer
= lpBuffer
;
228 Result
= GetComputerNameExW( NameType
, TempBuffer
, nSize
);
231 UnicodeString
.MaximumLength
= *nSize
* sizeof(WCHAR
);
232 UnicodeString
.Length
= *nSize
* sizeof(WCHAR
);
233 UnicodeString
.Buffer
= TempBuffer
;
235 RtlUnicodeStringToAnsiString (&AnsiString
,
240 HeapFree( GetProcessHeap(), 0, TempBuffer
);
249 GetComputerNameA (LPSTR lpBuffer
,
252 return GetComputerNameExA( ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
260 GetComputerNameW (LPWSTR lpBuffer
,
263 return GetComputerNameExW( ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
271 SetComputerNameA (LPCSTR lpComputerName
)
273 UNICODE_STRING ComputerName
;
276 RtlCreateUnicodeStringFromAsciiz (&ComputerName
,
277 (LPSTR
)lpComputerName
);
279 bResult
= SetComputerNameW (ComputerName
.Buffer
);
281 RtlFreeUnicodeString (&ComputerName
);
291 IsValidComputerName (LPCWSTR lpComputerName
)
297 p
= (PWCHAR
)lpComputerName
;
300 if (!(iswctype (*p
, _ALPHA
|| _DIGIT
) ||
324 Length
> MAX_COMPUTERNAME_LENGTH
)
335 SetComputerNameW (LPCWSTR lpComputerName
)
337 OBJECT_ATTRIBUTES ObjectAttributes
;
338 UNICODE_STRING KeyName
;
339 UNICODE_STRING ValueName
;
343 if (!IsValidComputerName (lpComputerName
))
345 SetLastError (ERROR_INVALID_PARAMETER
);
349 RtlInitUnicodeString (&KeyName
,
350 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName");
351 InitializeObjectAttributes (&ObjectAttributes
,
353 OBJ_CASE_INSENSITIVE
,
356 Status
= NtOpenKey (&KeyHandle
,
359 if (!NT_SUCCESS(Status
))
361 SetLastErrorByStatus (Status
);
365 RtlInitUnicodeString (&ValueName
,
368 Status
= NtSetValueKey (KeyHandle
,
372 (PVOID
)lpComputerName
,
373 (wcslen (lpComputerName
) + 1) * sizeof(WCHAR
));
374 if (!NT_SUCCESS(Status
))
377 SetLastErrorByStatus (Status
);
381 NtFlushKey (KeyHandle
);