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 BaseSetLastNTError (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
,
89 if (!NT_SUCCESS(Status
))
95 if (KeyInfo
->Type
!= REG_SZ
)
97 Status
= STATUS_UNSUCCESSFUL
;
101 if (!lpBuffer
|| *nSize
< (KeyInfo
->DataLength
/ sizeof(WCHAR
)))
104 Status
= STATUS_BUFFER_OVERFLOW
;
108 *nSize
= KeyInfo
->DataLength
/ sizeof(WCHAR
) - 1;
109 RtlCopyMemory(lpBuffer
, KeyInfo
->Data
, KeyInfo
->DataLength
);
110 lpBuffer
[*nSize
] = 0;
112 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo
);
117 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo
);
118 BaseSetLastNTError(Status
);
127 GetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
131 UNICODE_STRING ResultString
;
132 UNICODE_STRING DomainPart
;
133 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
140 case ComputerNameNetBIOS
:
141 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
142 L
"\\Control\\ComputerName\\ComputerName",
147 case ComputerNameDnsDomain
:
148 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
149 L
"\\Services\\Tcpip\\Parameters",
154 case ComputerNameDnsFullyQualified
:
155 ResultString
.Length
= 0;
156 ResultString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
);
157 ResultString
.Buffer
= lpBuffer
;
159 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
160 RtlInitUnicodeString(&DomainPart
, NULL
);
162 QueryTable
[0].Name
= L
"HostName";
163 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
164 QueryTable
[0].EntryContext
= &DomainPart
;
166 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
167 L
"\\Registry\\Machine\\System"
168 L
"\\CurrentControlSet\\Services\\Tcpip"
174 if (NT_SUCCESS(Status
))
176 Status
= RtlAppendUnicodeStringToString(&ResultString
, &DomainPart
);
177 HostSize
= DomainPart
.Length
;
179 if (!NT_SUCCESS(Status
))
184 RtlAppendUnicodeToString(&ResultString
, L
".");
185 RtlFreeUnicodeString(&DomainPart
);
187 RtlInitUnicodeString(&DomainPart
, NULL
);
188 QueryTable
[0].Name
= L
"Domain";
189 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
190 QueryTable
[0].EntryContext
= &DomainPart
;
192 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
193 L
"\\Registry\\Machine\\System"
194 L
"\\CurrentControlSet\\Services\\Tcpip"
200 if (NT_SUCCESS(Status
))
202 Status
= RtlAppendUnicodeStringToString(&ResultString
, &DomainPart
);
203 if ((!NT_SUCCESS(Status
)) || (!ret
))
205 *nSize
= HostSize
+ DomainPart
.Length
;
206 SetLastError(ERROR_MORE_DATA
);
207 RtlFreeUnicodeString(&DomainPart
);
210 RtlFreeUnicodeString(&DomainPart
);
211 *nSize
= ResultString
.Length
/ sizeof(WCHAR
) - 1;
217 case ComputerNameDnsHostname
:
218 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
219 L
"\\Services\\Tcpip\\Parameters",
224 case ComputerNamePhysicalDnsDomain
:
225 return GetComputerNameFromRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
226 L
"\\Services\\Tcpip\\Parameters",
232 case ComputerNamePhysicalDnsFullyQualified
:
233 return GetComputerNameExW(ComputerNameDnsFullyQualified
,
237 case ComputerNamePhysicalDnsHostname
:
238 return GetComputerNameExW(ComputerNameDnsHostname
,
242 case ComputerNamePhysicalNetBIOS
:
243 return GetComputerNameExW(ComputerNameNetBIOS
,
247 case ComputerNameMax
:
259 GetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
263 UNICODE_STRING UnicodeString
;
264 ANSI_STRING AnsiString
;
266 PWCHAR TempBuffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize
* sizeof(WCHAR
) );
270 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
274 AnsiString
.MaximumLength
= (USHORT
)*nSize
;
275 AnsiString
.Length
= 0;
276 AnsiString
.Buffer
= lpBuffer
;
278 Result
= GetComputerNameExW(NameType
, TempBuffer
, nSize
);
282 UnicodeString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
283 UnicodeString
.Length
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
284 UnicodeString
.Buffer
= TempBuffer
;
286 RtlUnicodeStringToAnsiString(&AnsiString
,
291 RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer
);
301 GetComputerNameA(LPSTR lpBuffer
, LPDWORD lpnSize
)
304 ret
= GetComputerNameExA(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
305 if(!ret
&& GetLastError() == ERROR_MORE_DATA
)
306 SetLastError(ERROR_BUFFER_OVERFLOW
);
316 GetComputerNameW(LPWSTR lpBuffer
, LPDWORD lpnSize
)
319 ret
=GetComputerNameExW(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
320 if(!ret
&& GetLastError() == ERROR_MORE_DATA
)
321 SetLastError(ERROR_BUFFER_OVERFLOW
);
331 IsValidComputerName(COMPUTER_NAME_FORMAT NameType
,
332 LPCWSTR lpComputerName
)
337 /* FIXME: do verification according to NameType */
340 p
= (PWCHAR
)lpComputerName
;
344 if (!(iswctype(*p
, _ALPHA
| _DIGIT
) || *p
== L
'!' || *p
== L
'@' || *p
== L
'#' ||
345 *p
== L
'$' || *p
== L
'%' || *p
== L
'^' || *p
== L
'&' || *p
== L
'\'' ||
346 *p
== L
')' || *p
== L
'(' || *p
== L
'.' || *p
== L
'-' || *p
== L
'_' ||
347 *p
== L
'{' || *p
== L
'}' || *p
== L
'~'))
354 if (Length
== 0 || Length
> MAX_COMPUTERNAME_LENGTH
)
363 SetComputerNameToRegistry(LPCWSTR RegistryKey
,
364 LPCWSTR ValueNameStr
,
367 OBJECT_ATTRIBUTES ObjectAttributes
;
368 UNICODE_STRING KeyName
;
369 UNICODE_STRING ValueName
;
373 RtlInitUnicodeString(&KeyName
, RegistryKey
);
374 InitializeObjectAttributes(&ObjectAttributes
,
376 OBJ_CASE_INSENSITIVE
,
380 Status
= NtOpenKey(&KeyHandle
,
383 if (!NT_SUCCESS(Status
))
385 BaseSetLastNTError(Status
);
389 RtlInitUnicodeString(&ValueName
, ValueNameStr
);
391 Status
= NtSetValueKey(KeyHandle
,
396 (wcslen (lpBuffer
) + 1) * sizeof(WCHAR
));
397 if (!NT_SUCCESS(Status
))
400 BaseSetLastNTError(Status
);
404 NtFlushKey(KeyHandle
);
416 SetComputerNameA(LPCSTR lpComputerName
)
418 return SetComputerNameExA(ComputerNamePhysicalNetBIOS
, lpComputerName
);
427 SetComputerNameW(LPCWSTR lpComputerName
)
429 return SetComputerNameExW(ComputerNamePhysicalNetBIOS
, lpComputerName
);
438 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
441 UNICODE_STRING Buffer
;
444 RtlCreateUnicodeStringFromAsciiz(&Buffer
, (LPSTR
)lpBuffer
);
446 bResult
= SetComputerNameExW(NameType
, Buffer
.Buffer
);
448 RtlFreeUnicodeString(&Buffer
);
459 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
462 if (!IsValidComputerName(NameType
, lpBuffer
))
464 SetLastError(ERROR_INVALID_PARAMETER
);
470 case ComputerNamePhysicalDnsDomain
:
471 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
472 L
"\\Services\\Tcpip\\Parameters",
476 case ComputerNamePhysicalDnsHostname
:
477 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
478 L
"\\Services\\Tcpip\\Parameters",
482 case ComputerNamePhysicalNetBIOS
:
483 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
484 L
"\\Control\\ComputerName\\ComputerName",
489 SetLastError (ERROR_INVALID_PARAMETER
);
500 DnsHostnameToComputerNameA(LPCSTR Hostname
,
506 DPRINT("(%s, %p, %p)\n", Hostname
, ComputerName
, nSize
);
508 if (!Hostname
|| !nSize
)
511 len
= lstrlenA(Hostname
);
513 if (len
> MAX_COMPUTERNAME_LENGTH
)
514 len
= MAX_COMPUTERNAME_LENGTH
;
522 if (!ComputerName
) return FALSE
;
524 memcpy(ComputerName
, Hostname
, len
);
525 ComputerName
[len
+ 1] = 0;
535 DnsHostnameToComputerNameW(LPCWSTR hostname
,
541 DPRINT("(%s, %p, %p): stub\n", hostname
, computername
, size
);
543 if (!hostname
|| !size
) return FALSE
;
544 len
= lstrlenW(hostname
);
546 if (len
> MAX_COMPUTERNAME_LENGTH
)
547 len
= MAX_COMPUTERNAME_LENGTH
;
554 if (!computername
) return FALSE
;
556 memcpy(computername
, hostname
, len
* sizeof(WCHAR
));
557 computername
[len
+ 1] = 0;
563 AddLocalAlternateComputerNameA(LPSTR lpName
, PNTSTATUS Status
)
571 AddLocalAlternateComputerNameW(LPWSTR lpName
, PNTSTATUS Status
)
579 EnumerateLocalComputerNamesA(PVOID pUnknown
, DWORD Size
, LPSTR lpBuffer
, LPDWORD lpnSize
)
582 return ERROR_CALL_NOT_IMPLEMENTED
;
587 EnumerateLocalComputerNamesW(PVOID pUnknown
, DWORD Size
, LPWSTR lpBuffer
, LPDWORD lpnSize
)
590 return ERROR_CALL_NOT_IMPLEMENTED
;
595 RemoveLocalAlternateComputerNameA(LPSTR lpName
, DWORD Unknown
)
598 return ERROR_CALL_NOT_IMPLEMENTED
;
603 RemoveLocalAlternateComputerNameW(LPWSTR lpName
, DWORD Unknown
)
606 return ERROR_CALL_NOT_IMPLEMENTED
;
614 SetLocalPrimaryComputerNameA(IN DWORD Unknown1
,
626 SetLocalPrimaryComputerNameW(IN DWORD Unknown1
,