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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * PURPOSE: Computer name functions
23 * FILE: lib/kernel32/misc/computername.c
24 * PROGRAMER: Eric Kohl
27 /* INCLUDES ******************************************************************/
35 /* FUNCTIONS *****************************************************************/
39 GetComputerNameFromRegistry(LPWSTR RegistryKey
,
44 PKEY_VALUE_PARTIAL_INFORMATION KeyInfo
;
45 OBJECT_ATTRIBUTES ObjectAttributes
;
46 UNICODE_STRING KeyName
;
47 UNICODE_STRING ValueName
;
53 RtlInitUnicodeString(&KeyName
,RegistryKey
);
54 InitializeObjectAttributes(&ObjectAttributes
,
60 Status
= ZwOpenKey(&KeyHandle
,
63 if (!NT_SUCCESS(Status
))
65 SetLastErrorByStatus (Status
);
69 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + *nSize
* sizeof(WCHAR
);
70 KeyInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, KeyInfoSize
);
74 SetLastError(ERROR_OUTOFMEMORY
);
78 RtlInitUnicodeString(&ValueName
,ValueNameStr
);
80 Status
= ZwQueryValueKey(KeyHandle
,
82 KeyValuePartialInformation
,
86 if (!NT_SUCCESS(Status
))
88 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo
);
91 SetLastErrorByStatus(Status
);
95 if (lpBuffer
&& *nSize
> (KeyInfo
->DataLength
/ sizeof(WCHAR
)))
97 *nSize
= KeyInfo
->DataLength
/ sizeof(WCHAR
) - 1;
102 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo
);
105 SetLastErrorByStatus(STATUS_BUFFER_OVERFLOW
);
109 RtlCopyMemory(lpBuffer
, KeyInfo
->Data
, *nSize
* sizeof(WCHAR
));
111 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo
);
122 GetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
126 UNICODE_STRING ResultString
;
127 UNICODE_STRING DomainPart
;
128 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
135 case ComputerNameNetBIOS
:
136 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
137 L
"\\Control\\ComputerName\\ComputerName",
142 case ComputerNameDnsDomain
:
143 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
144 L
"\\Services\\Tcpip\\Parameters",
149 case ComputerNameDnsFullyQualified
:
150 ResultString
.Length
= 0;
151 ResultString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
);
152 ResultString
.Buffer
= lpBuffer
;
154 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
155 RtlInitUnicodeString(&DomainPart
, NULL
);
157 QueryTable
[0].Name
= L
"HostName";
158 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
159 QueryTable
[0].EntryContext
= &DomainPart
;
161 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
162 L
"\\Registry\\Machine\\System"
163 L
"\\CurrentControlSet\\Services\\Tcpip"
169 if (NT_SUCCESS(Status
))
171 Status
= RtlAppendUnicodeStringToString(&ResultString
, &DomainPart
);
172 HostSize
= DomainPart
.Length
;
174 if (!NT_SUCCESS(Status
))
179 RtlAppendUnicodeToString(&ResultString
, L
".");
180 RtlFreeUnicodeString(&DomainPart
);
182 RtlInitUnicodeString(&DomainPart
, NULL
);
183 QueryTable
[0].Name
= L
"Domain";
184 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
185 QueryTable
[0].EntryContext
= &DomainPart
;
187 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
188 L
"\\Registry\\Machine\\System"
189 L
"\\CurrentControlSet\\Services\\Tcpip"
195 if (NT_SUCCESS(Status
))
197 Status
= RtlAppendUnicodeStringToString(&ResultString
, &DomainPart
);
198 if ((!NT_SUCCESS(Status
)) || (!ret
))
200 *nSize
= HostSize
+ DomainPart
.Length
;
201 SetLastError(ERROR_MORE_DATA
);
202 RtlFreeUnicodeString(&DomainPart
);
205 RtlFreeUnicodeString(&DomainPart
);
206 *nSize
= ResultString
.Length
/ sizeof(WCHAR
) - 1;
212 case ComputerNameDnsHostname
:
213 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
214 L
"\\Services\\Tcpip\\Parameters",
219 case ComputerNamePhysicalDnsDomain
:
220 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
221 L
"\\Services\\Tcpip\\Parameters",
227 case ComputerNamePhysicalDnsFullyQualified
:
228 return GetComputerNameExW(ComputerNameDnsFullyQualified
,
232 case ComputerNamePhysicalDnsHostname
:
233 return GetComputerNameExW(ComputerNameDnsHostname
,
237 case ComputerNamePhysicalNetBIOS
:
238 return GetComputerNameExW(ComputerNameNetBIOS
,
242 case ComputerNameMax
:
254 GetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
258 UNICODE_STRING UnicodeString
;
259 ANSI_STRING AnsiString
;
261 PWCHAR TempBuffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize
* sizeof(WCHAR
) );
265 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
269 AnsiString
.MaximumLength
= (USHORT
)*nSize
;
270 AnsiString
.Length
= 0;
271 AnsiString
.Buffer
= lpBuffer
;
273 Result
= GetComputerNameExW(NameType
, TempBuffer
, nSize
);
277 UnicodeString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
278 UnicodeString
.Length
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
279 UnicodeString
.Buffer
= TempBuffer
;
281 RtlUnicodeStringToAnsiString(&AnsiString
,
286 RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer
);
296 GetComputerNameA(LPSTR lpBuffer
, LPDWORD lpnSize
)
299 ret
= GetComputerNameExA(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
300 if(!ret
&& GetLastError() == ERROR_MORE_DATA
)
301 SetLastError(ERROR_BUFFER_OVERFLOW
);
311 GetComputerNameW(LPWSTR lpBuffer
, LPDWORD lpnSize
)
314 ret
=GetComputerNameExW(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
315 if(!ret
&& GetLastError() == ERROR_MORE_DATA
)
316 SetLastError(ERROR_BUFFER_OVERFLOW
);
326 IsValidComputerName(COMPUTER_NAME_FORMAT NameType
,
327 LPCWSTR lpComputerName
)
332 /* FIXME: do verification according to NameType */
335 p
= (PWCHAR
)lpComputerName
;
339 if (!(iswctype(*p
, _ALPHA
| _DIGIT
) || *p
== L
'!' || *p
== L
'@' || *p
== L
'#' ||
340 *p
== L
'$' || *p
== L
'%' || *p
== L
'^' || *p
== L
'&' || *p
== L
'\'' ||
341 *p
== L
')' || *p
== L
'(' || *p
== L
'.' || *p
== L
'-' || *p
== L
'_' ||
342 *p
== L
'{' || *p
== L
'}' || *p
== L
'~'))
349 if (Length
== 0 || Length
> MAX_COMPUTERNAME_LENGTH
)
358 SetComputerNameToRegistry(LPCWSTR RegistryKey
,
359 LPCWSTR ValueNameStr
,
362 OBJECT_ATTRIBUTES ObjectAttributes
;
363 UNICODE_STRING KeyName
;
364 UNICODE_STRING ValueName
;
368 RtlInitUnicodeString(&KeyName
, RegistryKey
);
369 InitializeObjectAttributes(&ObjectAttributes
,
371 OBJ_CASE_INSENSITIVE
,
375 Status
= NtOpenKey(&KeyHandle
,
378 if (!NT_SUCCESS(Status
))
380 SetLastErrorByStatus(Status
);
384 RtlInitUnicodeString(&ValueName
, ValueNameStr
);
386 Status
= NtSetValueKey(KeyHandle
,
391 (wcslen (lpBuffer
) + 1) * sizeof(WCHAR
));
392 if (!NT_SUCCESS(Status
))
395 SetLastErrorByStatus(Status
);
399 NtFlushKey(KeyHandle
);
411 SetComputerNameA(LPCSTR lpComputerName
)
413 return SetComputerNameExA(ComputerNamePhysicalNetBIOS
, lpComputerName
);
422 SetComputerNameW(LPCWSTR lpComputerName
)
424 return SetComputerNameExW(ComputerNamePhysicalNetBIOS
, lpComputerName
);
433 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
436 UNICODE_STRING Buffer
;
439 RtlCreateUnicodeStringFromAsciiz(&Buffer
, (LPSTR
)lpBuffer
);
441 bResult
= SetComputerNameExW(NameType
, Buffer
.Buffer
);
443 RtlFreeUnicodeString(&Buffer
);
454 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
457 if (!IsValidComputerName(NameType
, lpBuffer
))
459 SetLastError(ERROR_INVALID_PARAMETER
);
465 case ComputerNamePhysicalDnsDomain
:
466 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
467 L
"\\Services\\Tcpip\\Parameters",
471 case ComputerNamePhysicalDnsHostname
:
472 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
473 L
"\\Services\\Tcpip\\Parameters",
477 case ComputerNamePhysicalNetBIOS
:
478 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
479 L
"\\Control\\ComputerName\\ComputerName",
484 SetLastError (ERROR_INVALID_PARAMETER
);
495 DnsHostnameToComputerNameA(LPCSTR Hostname
,
501 DPRINT("(%s, %p, %p)\n", Hostname
, ComputerName
, nSize
);
503 if (!Hostname
|| !nSize
)
506 len
= lstrlenA(Hostname
);
508 if (len
> MAX_COMPUTERNAME_LENGTH
)
509 len
= MAX_COMPUTERNAME_LENGTH
;
517 if (!ComputerName
) return FALSE
;
519 memcpy(ComputerName
, Hostname
, len
);
520 ComputerName
[len
+ 1] = 0;
530 DnsHostnameToComputerNameW(LPCWSTR hostname
,
536 DPRINT("(%s, %p, %p): stub\n", hostname
, computername
, size
);
538 if (!hostname
|| !size
) return FALSE
;
539 len
= lstrlenW(hostname
);
541 if (len
> MAX_COMPUTERNAME_LENGTH
)
542 len
= MAX_COMPUTERNAME_LENGTH
;
549 if (!computername
) return FALSE
;
551 memcpy(computername
, hostname
, len
* sizeof(WCHAR
));
552 computername
[len
+ 1] = 0;