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
= NtOpenKey(&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
= NtQueryValueKey(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
)
305 ret
= GetComputerNameExA(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
306 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
307 SetLastError(ERROR_BUFFER_OVERFLOW
);
318 GetComputerNameW(LPWSTR lpBuffer
, LPDWORD lpnSize
)
321 ret
=GetComputerNameExW(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
322 if(!ret
&& GetLastError() == ERROR_MORE_DATA
)
323 SetLastError(ERROR_BUFFER_OVERFLOW
);
333 IsValidComputerName(COMPUTER_NAME_FORMAT NameType
,
334 LPCWSTR lpComputerName
)
339 /* FIXME: do verification according to NameType */
342 p
= (PWCHAR
)lpComputerName
;
346 if (!(iswctype(*p
, _ALPHA
| _DIGIT
) || *p
== L
'!' || *p
== L
'@' || *p
== L
'#' ||
347 *p
== L
'$' || *p
== L
'%' || *p
== L
'^' || *p
== L
'&' || *p
== L
'\'' ||
348 *p
== L
')' || *p
== L
'(' || *p
== L
'.' || *p
== L
'-' || *p
== L
'_' ||
349 *p
== L
'{' || *p
== L
'}' || *p
== L
'~'))
356 if (Length
== 0 || Length
> MAX_COMPUTERNAME_LENGTH
)
365 SetComputerNameToRegistry(LPCWSTR RegistryKey
,
366 LPCWSTR ValueNameStr
,
369 OBJECT_ATTRIBUTES ObjectAttributes
;
370 UNICODE_STRING KeyName
;
371 UNICODE_STRING ValueName
;
375 RtlInitUnicodeString(&KeyName
, RegistryKey
);
376 InitializeObjectAttributes(&ObjectAttributes
,
378 OBJ_CASE_INSENSITIVE
,
382 Status
= NtOpenKey(&KeyHandle
,
385 if (!NT_SUCCESS(Status
))
387 BaseSetLastNTError(Status
);
391 RtlInitUnicodeString(&ValueName
, ValueNameStr
);
393 Status
= NtSetValueKey(KeyHandle
,
398 (wcslen (lpBuffer
) + 1) * sizeof(WCHAR
));
399 if (!NT_SUCCESS(Status
))
402 BaseSetLastNTError(Status
);
406 NtFlushKey(KeyHandle
);
418 SetComputerNameA(LPCSTR lpComputerName
)
420 return SetComputerNameExA(ComputerNamePhysicalNetBIOS
, lpComputerName
);
429 SetComputerNameW(LPCWSTR lpComputerName
)
431 return SetComputerNameExW(ComputerNamePhysicalNetBIOS
, lpComputerName
);
440 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
443 UNICODE_STRING Buffer
;
446 RtlCreateUnicodeStringFromAsciiz(&Buffer
, (LPSTR
)lpBuffer
);
448 bResult
= SetComputerNameExW(NameType
, Buffer
.Buffer
);
450 RtlFreeUnicodeString(&Buffer
);
461 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
464 if (!IsValidComputerName(NameType
, lpBuffer
))
466 SetLastError(ERROR_INVALID_PARAMETER
);
472 case ComputerNamePhysicalDnsDomain
:
473 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
474 L
"\\Services\\Tcpip\\Parameters",
478 case ComputerNamePhysicalDnsHostname
:
479 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
480 L
"\\Services\\Tcpip\\Parameters",
484 case ComputerNamePhysicalNetBIOS
:
485 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
486 L
"\\Control\\ComputerName\\ComputerName",
491 SetLastError (ERROR_INVALID_PARAMETER
);
502 DnsHostnameToComputerNameA(LPCSTR Hostname
,
508 DPRINT("(%s, %p, %p)\n", Hostname
, ComputerName
, nSize
);
510 if (!Hostname
|| !nSize
)
513 len
= lstrlenA(Hostname
);
515 if (len
> MAX_COMPUTERNAME_LENGTH
)
516 len
= MAX_COMPUTERNAME_LENGTH
;
524 if (!ComputerName
) return FALSE
;
526 memcpy(ComputerName
, Hostname
, len
);
527 ComputerName
[len
+ 1] = 0;
537 DnsHostnameToComputerNameW(LPCWSTR hostname
,
543 DPRINT("(%s, %p, %p): stub\n", hostname
, computername
, size
);
545 if (!hostname
|| !size
) return FALSE
;
546 len
= lstrlenW(hostname
);
548 if (len
> MAX_COMPUTERNAME_LENGTH
)
549 len
= MAX_COMPUTERNAME_LENGTH
;
556 if (!computername
) return FALSE
;
558 memcpy(computername
, hostname
, len
* sizeof(WCHAR
));
559 computername
[len
+ 1] = 0;
565 AddLocalAlternateComputerNameA(LPSTR lpName
, PNTSTATUS Status
)
573 AddLocalAlternateComputerNameW(LPWSTR lpName
, PNTSTATUS Status
)
581 EnumerateLocalComputerNamesA(PVOID pUnknown
, DWORD Size
, LPSTR lpBuffer
, LPDWORD lpnSize
)
584 return ERROR_CALL_NOT_IMPLEMENTED
;
589 EnumerateLocalComputerNamesW(PVOID pUnknown
, DWORD Size
, LPWSTR lpBuffer
, LPDWORD lpnSize
)
592 return ERROR_CALL_NOT_IMPLEMENTED
;
597 RemoveLocalAlternateComputerNameA(LPSTR lpName
, DWORD Unknown
)
600 return ERROR_CALL_NOT_IMPLEMENTED
;
605 RemoveLocalAlternateComputerNameW(LPWSTR lpName
, DWORD Unknown
)
608 return ERROR_CALL_NOT_IMPLEMENTED
;
616 SetLocalPrimaryComputerNameA(IN DWORD Unknown1
,
628 SetLocalPrimaryComputerNameW(IN DWORD Unknown1
,