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: dll/win32/kernel32/client/compname.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
;
270 SetLastError(ERROR_INVALID_PARAMETER
);
274 TempBuffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, *nSize
* sizeof(WCHAR
));
277 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
281 AnsiString
.MaximumLength
= (USHORT
)*nSize
;
282 AnsiString
.Length
= 0;
283 AnsiString
.Buffer
= lpBuffer
;
285 Result
= GetComputerNameExW(NameType
, TempBuffer
, nSize
);
289 UnicodeString
.MaximumLength
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
290 UnicodeString
.Length
= (USHORT
)*nSize
* sizeof(WCHAR
) + sizeof(WCHAR
);
291 UnicodeString
.Buffer
= TempBuffer
;
293 RtlUnicodeStringToAnsiString(&AnsiString
,
298 RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer
);
308 GetComputerNameA(LPSTR lpBuffer
, LPDWORD lpnSize
)
312 ret
= GetComputerNameExA(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
313 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
314 SetLastError(ERROR_BUFFER_OVERFLOW
);
325 GetComputerNameW(LPWSTR lpBuffer
, LPDWORD lpnSize
)
328 ret
=GetComputerNameExW(ComputerNameNetBIOS
, lpBuffer
, lpnSize
);
329 if(!ret
&& GetLastError() == ERROR_MORE_DATA
)
330 SetLastError(ERROR_BUFFER_OVERFLOW
);
340 IsValidComputerName(COMPUTER_NAME_FORMAT NameType
,
341 LPCWSTR lpComputerName
)
346 /* FIXME: do verification according to NameType */
349 p
= (PWCHAR
)lpComputerName
;
353 if (!(iswctype(*p
, _ALPHA
| _DIGIT
) || *p
== L
'!' || *p
== L
'@' || *p
== L
'#' ||
354 *p
== L
'$' || *p
== L
'%' || *p
== L
'^' || *p
== L
'&' || *p
== L
'\'' ||
355 *p
== L
')' || *p
== L
'(' || *p
== L
'.' || *p
== L
'-' || *p
== L
'_' ||
356 *p
== L
'{' || *p
== L
'}' || *p
== L
'~'))
363 if (Length
== 0 || Length
> MAX_COMPUTERNAME_LENGTH
)
372 SetComputerNameToRegistry(LPCWSTR RegistryKey
,
373 LPCWSTR ValueNameStr
,
376 OBJECT_ATTRIBUTES ObjectAttributes
;
377 UNICODE_STRING KeyName
;
378 UNICODE_STRING ValueName
;
382 RtlInitUnicodeString(&KeyName
, RegistryKey
);
383 InitializeObjectAttributes(&ObjectAttributes
,
385 OBJ_CASE_INSENSITIVE
,
389 Status
= NtOpenKey(&KeyHandle
,
392 if (!NT_SUCCESS(Status
))
394 BaseSetLastNTError(Status
);
398 RtlInitUnicodeString(&ValueName
, ValueNameStr
);
400 Status
= NtSetValueKey(KeyHandle
,
405 (wcslen (lpBuffer
) + 1) * sizeof(WCHAR
));
406 if (!NT_SUCCESS(Status
))
409 BaseSetLastNTError(Status
);
413 NtFlushKey(KeyHandle
);
425 SetComputerNameA(LPCSTR lpComputerName
)
427 return SetComputerNameExA(ComputerNamePhysicalNetBIOS
, lpComputerName
);
436 SetComputerNameW(LPCWSTR lpComputerName
)
438 return SetComputerNameExW(ComputerNamePhysicalNetBIOS
, lpComputerName
);
447 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType
,
450 UNICODE_STRING Buffer
;
453 RtlCreateUnicodeStringFromAsciiz(&Buffer
, (LPSTR
)lpBuffer
);
455 bResult
= SetComputerNameExW(NameType
, Buffer
.Buffer
);
457 RtlFreeUnicodeString(&Buffer
);
468 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType
,
471 if (!IsValidComputerName(NameType
, lpBuffer
))
473 SetLastError(ERROR_INVALID_PARAMETER
);
479 case ComputerNamePhysicalDnsDomain
:
480 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
481 L
"\\Services\\Tcpip\\Parameters",
485 case ComputerNamePhysicalDnsHostname
:
486 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
487 L
"\\Services\\Tcpip\\Parameters",
491 case ComputerNamePhysicalNetBIOS
:
492 return SetComputerNameToRegistry(L
"\\Registry\\Machine\\System\\CurrentControlSet"
493 L
"\\Control\\ComputerName\\ComputerName",
498 SetLastError (ERROR_INVALID_PARAMETER
);
509 DnsHostnameToComputerNameA(LPCSTR Hostname
,
515 DPRINT("(%s, %p, %p)\n", Hostname
, ComputerName
, nSize
);
517 if (!Hostname
|| !nSize
)
520 len
= lstrlenA(Hostname
);
522 if (len
> MAX_COMPUTERNAME_LENGTH
)
523 len
= MAX_COMPUTERNAME_LENGTH
;
531 if (!ComputerName
) return FALSE
;
533 memcpy(ComputerName
, Hostname
, len
);
534 ComputerName
[len
+ 1] = 0;
544 DnsHostnameToComputerNameW(LPCWSTR hostname
,
550 DPRINT("(%s, %p, %p): stub\n", hostname
, computername
, size
);
552 if (!hostname
|| !size
) return FALSE
;
553 len
= lstrlenW(hostname
);
555 if (len
> MAX_COMPUTERNAME_LENGTH
)
556 len
= MAX_COMPUTERNAME_LENGTH
;
563 if (!computername
) return FALSE
;
565 memcpy(computername
, hostname
, len
* sizeof(WCHAR
));
566 computername
[len
+ 1] = 0;
572 AddLocalAlternateComputerNameA(LPSTR lpName
, PNTSTATUS Status
)
580 AddLocalAlternateComputerNameW(LPWSTR lpName
, PNTSTATUS Status
)
588 EnumerateLocalComputerNamesA(PVOID pUnknown
, DWORD Size
, LPSTR lpBuffer
, LPDWORD lpnSize
)
591 return ERROR_CALL_NOT_IMPLEMENTED
;
596 EnumerateLocalComputerNamesW(PVOID pUnknown
, DWORD Size
, LPWSTR lpBuffer
, LPDWORD lpnSize
)
599 return ERROR_CALL_NOT_IMPLEMENTED
;
604 RemoveLocalAlternateComputerNameA(LPSTR lpName
, DWORD Unknown
)
607 return ERROR_CALL_NOT_IMPLEMENTED
;
612 RemoveLocalAlternateComputerNameW(LPWSTR lpName
, DWORD Unknown
)
615 return ERROR_CALL_NOT_IMPLEMENTED
;
623 SetLocalPrimaryComputerNameA(IN DWORD Unknown1
,
635 SetLocalPrimaryComputerNameW(IN DWORD Unknown1
,