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 (ekohl@rz-online.de)
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 (*nSize
> (KeyInfo
->DataLength
/ sizeof(WCHAR
)))
97 *nSize
= KeyInfo
->DataLength
/ sizeof(WCHAR
) - 1;
101 RtlCopyMemory(lpBuffer
, KeyInfo
->Data
, *nSize
* sizeof(WCHAR
));
103 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo
);
114 GetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
118 UNICODE_STRING ResultString
;
119 UNICODE_STRING DomainPart
;
120 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
127 case ComputerNameNetBIOS
:
128 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
129 L
"\\Control\\ComputerName\\ComputerName",
134 case ComputerNameDnsDomain
:
135 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
136 L
"\\Services\\Tcpip\\Parameters",
141 case ComputerNameDnsFullyQualified
:
142 ResultString
.Length
= 0;
143 ResultString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
);
144 ResultString
.Buffer
= lpBuffer
;
146 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
147 RtlInitUnicodeString(&DomainPart
, NULL
);
149 QueryTable
[0].Name
= L
"HostName";
150 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
151 QueryTable
[0].EntryContext
= &DomainPart
;
153 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
154 L
"\\Registry\\Machine\\System"
155 L
"\\CurrentControlSet\\Services\\Tcpip"
161 if (NT_SUCCESS(Status
))
163 Status
= RtlAppendUnicodeStringToString(&ResultString
, &DomainPart
);
164 HostSize
= DomainPart
.Length
;
166 if (!NT_SUCCESS(Status
))
171 RtlAppendUnicodeToString(&ResultString
, L
".");
172 RtlFreeUnicodeString(&DomainPart
);
174 RtlInitUnicodeString(&DomainPart
, NULL
);
175 QueryTable
[0].Name
= L
"Domain";
176 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
177 QueryTable
[0].EntryContext
= &DomainPart
;
179 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
180 L
"\\Registry\\Machine\\System"
181 L
"\\CurrentControlSet\\Services\\Tcpip"
187 if (NT_SUCCESS(Status
))
189 Status
= RtlAppendUnicodeStringToString(&ResultString
, &DomainPart
);
190 if ((!NT_SUCCESS(Status
)) || (!ret
))
192 *nSize
= HostSize
+ DomainPart
.Length
;
193 SetLastError(ERROR_MORE_DATA
);
194 RtlFreeUnicodeString(&DomainPart
);
197 RtlFreeUnicodeString(&DomainPart
);
198 *nSize
= ResultString
.Length
/ sizeof(WCHAR
) - 1;
204 case ComputerNameDnsHostname
:
205 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
206 L
"\\Services\\Tcpip\\Parameters",
211 case ComputerNamePhysicalDnsDomain
:
212 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
213 L
"\\Services\\Tcpip\\Parameters",
219 case ComputerNamePhysicalDnsFullyQualified
:
220 return GetComputerNameExW(ComputerNameDnsFullyQualified
,
224 case ComputerNamePhysicalDnsHostname
:
225 return GetComputerNameExW(ComputerNameDnsHostname
,
229 case ComputerNamePhysicalNetBIOS
:
230 return GetComputerNameExW(ComputerNameNetBIOS
,
234 case ComputerNameMax
:
246 GetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
250 UNICODE_STRING UnicodeString
;
251 ANSI_STRING AnsiString
;
253 PWCHAR TempBuffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize
* sizeof(WCHAR
) );
257 return ERROR_OUTOFMEMORY
;
260 AnsiString
.MaximumLength
= (USHORT
)*nSize
;
261 AnsiString
.Length
= 0;
262 AnsiString
.Buffer
= lpBuffer
;
264 Result
= GetComputerNameExW(NameType
, TempBuffer
, nSize
);
268 UnicodeString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
269 UnicodeString
.Length
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
270 UnicodeString
.Buffer
= TempBuffer
;
272 RtlUnicodeStringToAnsiString(&AnsiString
,
277 RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer
);
287 GetComputerNameA(LPSTR lpBuffer
, LPDWORD lpnSize
)
289 return GetComputerNameExA(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
298 GetComputerNameW(LPWSTR lpBuffer
, LPDWORD lpnSize
)
300 return GetComputerNameExW(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
309 IsValidComputerName(COMPUTER_NAME_FORMAT NameType
,
310 LPCWSTR lpComputerName
)
315 /* FIXME: do verification according to NameType */
318 p
= (PWCHAR
)lpComputerName
;
322 if (!(iswctype(*p
, _ALPHA
| _DIGIT
) || *p
== L
'!' || *p
== L
'@' || *p
== L
'#' ||
323 *p
== L
'$' || *p
== L
'%' || *p
== L
'^' || *p
== L
'&' || *p
== L
'\'' ||
324 *p
== L
')' || *p
== L
'(' || *p
== L
'.' || *p
== L
'-' || *p
== L
'_' ||
325 *p
== L
'{' || *p
== L
'}' || *p
== L
'~'))
332 if (Length
== 0 || Length
> MAX_COMPUTERNAME_LENGTH
)
341 SetComputerNameToRegistry(LPCWSTR RegistryKey
,
342 LPCWSTR ValueNameStr
,
345 OBJECT_ATTRIBUTES ObjectAttributes
;
346 UNICODE_STRING KeyName
;
347 UNICODE_STRING ValueName
;
351 RtlInitUnicodeString(&KeyName
, RegistryKey
);
352 InitializeObjectAttributes(&ObjectAttributes
,
354 OBJ_CASE_INSENSITIVE
,
358 Status
= NtOpenKey(&KeyHandle
,
361 if (!NT_SUCCESS(Status
))
363 SetLastErrorByStatus(Status
);
367 RtlInitUnicodeString(&ValueName
, ValueNameStr
);
369 Status
= NtSetValueKey(KeyHandle
,
374 (wcslen (lpBuffer
) + 1) * sizeof(WCHAR
));
375 if (!NT_SUCCESS(Status
))
378 SetLastErrorByStatus(Status
);
382 NtFlushKey(KeyHandle
);
394 SetComputerNameA(LPCSTR lpComputerName
)
396 return SetComputerNameExA(ComputerNamePhysicalNetBIOS
, lpComputerName
);
405 SetComputerNameW(LPCWSTR lpComputerName
)
407 return SetComputerNameExW(ComputerNamePhysicalNetBIOS
, lpComputerName
);
416 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
419 UNICODE_STRING Buffer
;
422 RtlCreateUnicodeStringFromAsciiz(&Buffer
, (LPSTR
)lpBuffer
);
424 bResult
= SetComputerNameExW(NameType
, Buffer
.Buffer
);
426 RtlFreeUnicodeString(&Buffer
);
437 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
440 if (!IsValidComputerName(NameType
, lpBuffer
))
442 SetLastError(ERROR_INVALID_PARAMETER
);
448 case ComputerNamePhysicalDnsDomain
:
449 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
450 L
"\\Services\\Tcpip\\Parameters",
454 case ComputerNamePhysicalDnsHostname
:
455 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
456 L
"\\Services\\Tcpip\\Parameters",
460 case ComputerNamePhysicalNetBIOS
:
461 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
462 L
"\\Control\\ComputerName\\ComputerName",
467 SetLastError (ERROR_INVALID_PARAMETER
);
478 DnsHostnameToComputerNameA(LPCSTR Hostname
,
484 DPRINT("(%s, %p, %p)\n", Hostname
, ComputerName
, nSize
);
486 if (!Hostname
|| !nSize
)
489 len
= lstrlenA(Hostname
);
491 if (len
> MAX_COMPUTERNAME_LENGTH
)
492 len
= MAX_COMPUTERNAME_LENGTH
;
500 if (!ComputerName
) return FALSE
;
502 memcpy(ComputerName
, Hostname
, len
);
503 ComputerName
[len
+ 1] = 0;
513 DnsHostnameToComputerNameW(LPCWSTR hostname
,
519 DPRINT("(%s, %p, %p): stub\n", hostname
, computername
, size
);
521 if (!hostname
|| !size
) return FALSE
;
522 len
= lstrlenW(hostname
);
524 if (len
> MAX_COMPUTERNAME_LENGTH
)
525 len
= MAX_COMPUTERNAME_LENGTH
;
532 if (!computername
) return FALSE
;
534 memcpy(computername
, hostname
, len
* sizeof(WCHAR
));
535 computername
[len
+ 1] = 0;