diff --git a/reactos/dll/win32/kernel32/misc/computername.c b/reactos/dll/win32/kernel32/misc/computername.c index 663abed9fbd..79bcb3153aa 100644 --- a/reactos/dll/win32/kernel32/misc/computername.c +++ b/reactos/dll/win32/kernel32/misc/computername.c @@ -34,10 +34,13 @@ /* FUNCTIONS *****************************************************************/ -static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey, - LPWSTR ValueNameStr, - LPWSTR lpBuffer, - LPDWORD nSize ) { +static +BOOL +GetComputerNameFromRegistry(LPWSTR RegistryKey, + LPWSTR ValueNameStr, + LPWSTR lpBuffer, + LPDWORD nSize ) +{ PKEY_VALUE_PARTIAL_INFORMATION KeyInfo; OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING KeyName; @@ -47,65 +50,58 @@ static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey, ULONG ReturnSize; NTSTATUS Status; - RtlInitUnicodeString (&KeyName,RegistryKey); - InitializeObjectAttributes (&ObjectAttributes, - &KeyName, - OBJ_CASE_INSENSITIVE, - NULL, - NULL); - Status = ZwOpenKey (&KeyHandle, - KEY_READ, - &ObjectAttributes); + RtlInitUnicodeString(&KeyName,RegistryKey); + InitializeObjectAttributes(&ObjectAttributes, + &KeyName, + OBJ_CASE_INSENSITIVE, + NULL, + NULL); + + Status = ZwOpenKey(&KeyHandle, + KEY_READ, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { SetLastErrorByStatus (Status); return FALSE; } - KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + - *nSize * sizeof(WCHAR); - KeyInfo = RtlAllocateHeap (RtlGetProcessHeap (), - 0, - KeyInfoSize); + KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + *nSize * sizeof(WCHAR); + KeyInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, KeyInfoSize); if (KeyInfo == NULL) { - ZwClose (KeyHandle); - SetLastError (ERROR_OUTOFMEMORY); + ZwClose(KeyHandle); + SetLastError(ERROR_OUTOFMEMORY); return FALSE; } - RtlInitUnicodeString (&ValueName,ValueNameStr); + RtlInitUnicodeString(&ValueName,ValueNameStr); - Status = ZwQueryValueKey (KeyHandle, - &ValueName, - KeyValuePartialInformation, - KeyInfo, - KeyInfoSize, - &ReturnSize); + Status = ZwQueryValueKey(KeyHandle, + &ValueName, + KeyValuePartialInformation, + KeyInfo, + KeyInfoSize, + &ReturnSize); if (!NT_SUCCESS(Status)) { - RtlFreeHeap (RtlGetProcessHeap (), - 0, - KeyInfo); - ZwClose (KeyHandle); + RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo); + ZwClose(KeyHandle); *nSize = ReturnSize; - SetLastErrorByStatus (Status); + SetLastErrorByStatus(Status); return FALSE; } - if( *nSize > (KeyInfo->DataLength / sizeof(WCHAR)) ) { + if (*nSize > (KeyInfo->DataLength / sizeof(WCHAR))) + { *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1; lpBuffer[*nSize] = 0; } - RtlCopyMemory (lpBuffer, - KeyInfo->Data, - *nSize * sizeof(WCHAR)); + RtlCopyMemory(lpBuffer, KeyInfo->Data, *nSize * sizeof(WCHAR)); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - KeyInfo); - ZwClose (KeyHandle); + RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo); + ZwClose(KeyHandle); return TRUE; } @@ -113,12 +109,11 @@ static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey, /* * @implemented */ -BOOL WINAPI -GetComputerNameExW ( - COMPUTER_NAME_FORMAT NameType, - LPWSTR lpBuffer, - LPDWORD nSize - ) +BOOL +WINAPI +GetComputerNameExW(COMPUTER_NAME_FORMAT NameType, + LPWSTR lpBuffer, + LPDWORD nSize) { UNICODE_STRING ResultString; UNICODE_STRING DomainPart; @@ -127,51 +122,31 @@ GetComputerNameExW ( BOOL ret = TRUE; DWORD HostSize; - switch( NameType ) { - case ComputerNameNetBIOS: - return GetComputerNameFromRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Control\\ComputerName\\ComputerName", - L"ComputerName", - lpBuffer, - nSize ); + switch (NameType) + { + case ComputerNameNetBIOS: + return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Control\\ComputerName\\ComputerName", + L"ComputerName", + lpBuffer, + nSize); - case ComputerNameDnsDomain: - return GetComputerNameFromRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Domain", - lpBuffer, - nSize ); + case ComputerNameDnsDomain: + return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Domain", + lpBuffer, + nSize); - case ComputerNameDnsFullyQualified: - ResultString.Length = 0; - ResultString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR); - ResultString.Buffer = lpBuffer; - - RtlZeroMemory(QueryTable, sizeof(QueryTable)); - RtlInitUnicodeString(&DomainPart, NULL); - QueryTable[0].Name = L"HostName"; - QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; - QueryTable[0].EntryContext = &DomainPart; - - Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE, - L"\\Registry\\Machine\\System" - L"\\CurrentControlSet\\Services\\Tcpip" - L"\\Parameters", - QueryTable, NULL, NULL); - - if( NT_SUCCESS(Status) ) { - Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); - HostSize = DomainPart.Length; - if (!NT_SUCCESS(Status)) { - ret = FALSE; - } - RtlAppendUnicodeToString(&ResultString, L"."); - RtlFreeUnicodeString(&DomainPart); + case ComputerNameDnsFullyQualified: + ResultString.Length = 0; + ResultString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR); + ResultString.Buffer = lpBuffer; + RtlZeroMemory(QueryTable, sizeof(QueryTable)); RtlInitUnicodeString(&DomainPart, NULL); - QueryTable[0].Name = L"Domain"; + + QueryTable[0].Name = L"HostName"; QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; QueryTable[0].EntryContext = &DomainPart; @@ -179,52 +154,85 @@ GetComputerNameExW ( L"\\Registry\\Machine\\System" L"\\CurrentControlSet\\Services\\Tcpip" L"\\Parameters", - QueryTable, NULL, NULL); + QueryTable, + NULL, + NULL); - if( NT_SUCCESS(Status) ) { + if (NT_SUCCESS(Status)) + { Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); - if ( (!NT_SUCCESS(Status)) || (!ret)) { - *nSize = HostSize + DomainPart.Length; - SetLastError(ERROR_MORE_DATA); - RtlFreeUnicodeString(&DomainPart); - return FALSE; + HostSize = DomainPart.Length; + + if (!NT_SUCCESS(Status)) + { + ret = FALSE; } + + RtlAppendUnicodeToString(&ResultString, L"."); RtlFreeUnicodeString(&DomainPart); - *nSize = ResultString.Length / sizeof(WCHAR) - 1; - return TRUE; + + RtlInitUnicodeString(&DomainPart, NULL); + QueryTable[0].Name = L"Domain"; + QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; + QueryTable[0].EntryContext = &DomainPart; + + Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE, + L"\\Registry\\Machine\\System" + L"\\CurrentControlSet\\Services\\Tcpip" + L"\\Parameters", + QueryTable, + NULL, + NULL); + + if (NT_SUCCESS(Status)) + { + Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); + if ((!NT_SUCCESS(Status)) || (!ret)) + { + *nSize = HostSize + DomainPart.Length; + SetLastError(ERROR_MORE_DATA); + RtlFreeUnicodeString(&DomainPart); + return FALSE; + } + RtlFreeUnicodeString(&DomainPart); + *nSize = ResultString.Length / sizeof(WCHAR) - 1; + return TRUE; + } } - } - return FALSE; + return FALSE; - case ComputerNameDnsHostname: - return GetComputerNameFromRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Hostname", - lpBuffer, - nSize ); + case ComputerNameDnsHostname: + return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Hostname", + lpBuffer, + nSize); - case ComputerNamePhysicalDnsDomain: - return GetComputerNameFromRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Domain", - lpBuffer, - nSize ); + case ComputerNamePhysicalDnsDomain: + return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Domain", + lpBuffer, + nSize); - /* XXX Redo these */ - case ComputerNamePhysicalDnsFullyQualified: - return GetComputerNameExW( ComputerNameDnsFullyQualified, - lpBuffer, nSize ); - case ComputerNamePhysicalDnsHostname: - return GetComputerNameExW( ComputerNameDnsHostname, - lpBuffer, nSize ); - case ComputerNamePhysicalNetBIOS: - return GetComputerNameExW( ComputerNameNetBIOS, - lpBuffer, nSize ); + /* XXX Redo these */ + case ComputerNamePhysicalDnsFullyQualified: + return GetComputerNameExW(ComputerNameDnsFullyQualified, + lpBuffer, + nSize); - case ComputerNameMax: - return FALSE; + case ComputerNamePhysicalDnsHostname: + return GetComputerNameExW(ComputerNameDnsHostname, + lpBuffer, + nSize); + + case ComputerNamePhysicalNetBIOS: + return GetComputerNameExW(ComputerNameNetBIOS, + lpBuffer, + nSize); + + case ComputerNameMax: + return FALSE; } return FALSE; @@ -235,18 +243,17 @@ GetComputerNameExW ( */ BOOL WINAPI -GetComputerNameExA ( - COMPUTER_NAME_FORMAT NameType, - LPSTR lpBuffer, - LPDWORD nSize - ) +GetComputerNameExA(COMPUTER_NAME_FORMAT NameType, + LPSTR lpBuffer, + LPDWORD nSize) { UNICODE_STRING UnicodeString; ANSI_STRING AnsiString; BOOL Result; PWCHAR TempBuffer = RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize * sizeof(WCHAR) ); - if( !TempBuffer ) { + if (!TempBuffer) + { return ERROR_OUTOFMEMORY; } @@ -254,19 +261,20 @@ GetComputerNameExA ( AnsiString.Length = 0; AnsiString.Buffer = lpBuffer; - Result = GetComputerNameExW( NameType, TempBuffer, nSize ); + Result = GetComputerNameExW(NameType, TempBuffer, nSize); - if( Result ) { + if (Result) + { UnicodeString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR); UnicodeString.Length = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR); UnicodeString.Buffer = TempBuffer; - RtlUnicodeStringToAnsiString (&AnsiString, - &UnicodeString, - FALSE); + RtlUnicodeStringToAnsiString(&AnsiString, + &UnicodeString, + FALSE); } - RtlFreeHeap( RtlGetProcessHeap(), 0, TempBuffer ); + RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer); return Result; } @@ -274,64 +282,65 @@ GetComputerNameExA ( /* * @implemented */ -BOOL WINAPI -GetComputerNameA (LPSTR lpBuffer, - LPDWORD lpnSize) +BOOL +WINAPI +GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize) { - return GetComputerNameExA( ComputerNameNetBIOS, lpBuffer, lpnSize ); + return GetComputerNameExA(ComputerNameNetBIOS, lpBuffer, lpnSize); } /* * @implemented */ -BOOL WINAPI -GetComputerNameW (LPWSTR lpBuffer, - LPDWORD lpnSize) +BOOL +WINAPI +GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize) { - return GetComputerNameExW( ComputerNameNetBIOS, lpBuffer, lpnSize ); + return GetComputerNameExW(ComputerNameNetBIOS, lpBuffer, lpnSize); } /* * @implemented */ -static BOOL -IsValidComputerName ( - COMPUTER_NAME_FORMAT NameType, - LPCWSTR lpComputerName) +static +BOOL +IsValidComputerName(COMPUTER_NAME_FORMAT NameType, + LPCWSTR lpComputerName) { - PWCHAR p; - ULONG Length; + PWCHAR p; + ULONG Length; - /* FIXME: do verification according to NameType */ + /* FIXME: do verification according to NameType */ - Length = 0; - p = (PWCHAR)lpComputerName; - while (*p != 0) + Length = 0; + p = (PWCHAR)lpComputerName; + + while (*p != 0) { - if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' || - *p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L'\'' || - *p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' || - *p == L'{' || *p == L'}' || *p == L'~')) - return FALSE; + if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' || + *p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L'\'' || + *p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' || + *p == L'{' || *p == L'}' || *p == L'~')) + return FALSE; - Length++; - p++; + Length++; + p++; } - if (Length == 0 || - Length > MAX_COMPUTERNAME_LENGTH) - return FALSE; + if (Length == 0 || Length > MAX_COMPUTERNAME_LENGTH) + return FALSE; - return TRUE; + return TRUE; } -static BOOL SetComputerNameToRegistry( - LPCWSTR RegistryKey, - LPCWSTR ValueNameStr, - LPCWSTR lpBuffer) +static +BOOL +SetComputerNameToRegistry(LPCWSTR RegistryKey, + LPCWSTR ValueNameStr, + LPCWSTR lpBuffer) { OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING KeyName; @@ -339,39 +348,39 @@ static BOOL SetComputerNameToRegistry( HANDLE KeyHandle; NTSTATUS Status; - RtlInitUnicodeString (&KeyName, RegistryKey); - InitializeObjectAttributes (&ObjectAttributes, - &KeyName, - OBJ_CASE_INSENSITIVE, - NULL, - NULL ); + RtlInitUnicodeString(&KeyName, RegistryKey); + InitializeObjectAttributes(&ObjectAttributes, + &KeyName, + OBJ_CASE_INSENSITIVE, + NULL, + NULL); - Status = NtOpenKey (&KeyHandle, - KEY_WRITE, - &ObjectAttributes); + Status = NtOpenKey(&KeyHandle, + KEY_WRITE, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { - SetLastErrorByStatus (Status); + SetLastErrorByStatus(Status); return FALSE; } - RtlInitUnicodeString (&ValueName, ValueNameStr); + RtlInitUnicodeString(&ValueName, ValueNameStr); - Status = NtSetValueKey (KeyHandle, - &ValueName, - 0, - REG_SZ, - (PVOID)lpBuffer, - (wcslen (lpBuffer) + 1) * sizeof(WCHAR)); + Status = NtSetValueKey(KeyHandle, + &ValueName, + 0, + REG_SZ, + (PVOID)lpBuffer, + (wcslen (lpBuffer) + 1) * sizeof(WCHAR)); if (!NT_SUCCESS(Status)) { - ZwClose (KeyHandle); - SetLastErrorByStatus (Status); + ZwClose(KeyHandle); + SetLastErrorByStatus(Status); return FALSE; } - NtFlushKey (KeyHandle); - ZwClose (KeyHandle); + NtFlushKey(KeyHandle); + ZwClose(KeyHandle); return TRUE; } @@ -380,40 +389,41 @@ static BOOL SetComputerNameToRegistry( /* * @implemented */ -BOOL WINAPI -SetComputerNameA (LPCSTR lpComputerName) +BOOL +WINAPI +SetComputerNameA(LPCSTR lpComputerName) { - return SetComputerNameExA( ComputerNamePhysicalNetBIOS, lpComputerName ); + return SetComputerNameExA(ComputerNamePhysicalNetBIOS, lpComputerName); } /* * @implemented */ -BOOL WINAPI -SetComputerNameW (LPCWSTR lpComputerName) +BOOL +WINAPI +SetComputerNameW(LPCWSTR lpComputerName) { - return SetComputerNameExW( ComputerNamePhysicalNetBIOS, lpComputerName ); + return SetComputerNameExW(ComputerNamePhysicalNetBIOS, lpComputerName); } /* * @implemented */ -BOOL WINAPI -SetComputerNameExA ( - COMPUTER_NAME_FORMAT NameType, - LPCSTR lpBuffer) +BOOL +WINAPI +SetComputerNameExA(COMPUTER_NAME_FORMAT NameType, + LPCSTR lpBuffer) { UNICODE_STRING Buffer; BOOL bResult; - RtlCreateUnicodeStringFromAsciiz (&Buffer, - (LPSTR)lpBuffer); + RtlCreateUnicodeStringFromAsciiz(&Buffer, (LPSTR)lpBuffer); - bResult = SetComputerNameExW (NameType, Buffer.Buffer); + bResult = SetComputerNameExW(NameType, Buffer.Buffer); - RtlFreeUnicodeString (&Buffer); + RtlFreeUnicodeString(&Buffer); return bResult; } @@ -422,43 +432,41 @@ SetComputerNameExA ( /* * @implemented */ -BOOL WINAPI -SetComputerNameExW ( - COMPUTER_NAME_FORMAT NameType, - LPCWSTR lpBuffer) +BOOL +WINAPI +SetComputerNameExW(COMPUTER_NAME_FORMAT NameType, + LPCWSTR lpBuffer) { - if (!IsValidComputerName (NameType, lpBuffer)) + if (!IsValidComputerName(NameType, lpBuffer)) { - SetLastError (ERROR_INVALID_PARAMETER); - return FALSE; + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; } - switch( NameType ) { - case ComputerNamePhysicalDnsDomain: - return SetComputerNameToRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Domain", - lpBuffer ); + switch( NameType ) + { + case ComputerNamePhysicalDnsDomain: + return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Domain", + lpBuffer); - case ComputerNamePhysicalDnsHostname: - return SetComputerNameToRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Hostname", - lpBuffer ); + case ComputerNamePhysicalDnsHostname: + return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Hostname", + lpBuffer); - case ComputerNamePhysicalNetBIOS: - return SetComputerNameToRegistry - ( L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Control\\ComputerName\\ComputerName", - L"ComputerName", - lpBuffer ); + case ComputerNamePhysicalNetBIOS: + return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Control\\ComputerName\\ComputerName", + L"ComputerName", + lpBuffer); - default: - SetLastError (ERROR_INVALID_PARAMETER); - return FALSE; - } + default: + SetLastError (ERROR_INVALID_PARAMETER); + return FALSE; + } } @@ -491,7 +499,7 @@ DnsHostnameToComputerNameA(LPCSTR Hostname, if (!ComputerName) return FALSE; - memcpy( ComputerName, Hostname, len ); + memcpy(ComputerName, Hostname, len); ComputerName[len + 1] = 0; return TRUE; } @@ -502,11 +510,9 @@ DnsHostnameToComputerNameA(LPCSTR Hostname, */ BOOL WINAPI -DnsHostnameToComputerNameW ( - LPCWSTR hostname, - LPWSTR computername, - LPDWORD size - ) +DnsHostnameToComputerNameW(LPCWSTR hostname, + LPWSTR computername, + LPDWORD size) { DWORD len; @@ -525,7 +531,7 @@ DnsHostnameToComputerNameW ( } if (!computername) return FALSE; - memcpy( computername, hostname, len * sizeof(WCHAR) ); + memcpy(computername, hostname, len * sizeof(WCHAR)); computername[len + 1] = 0; return TRUE; }