GetComputerNameFromRegistry(LPWSTR RegistryKey,
LPWSTR ValueNameStr,
LPWSTR lpBuffer,
- LPDWORD nSize )
+ LPDWORD nSize)
{
PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
OBJECT_ATTRIBUTES ObjectAttributes;
ULONG ReturnSize;
NTSTATUS Status;
- RtlInitUnicodeString(&KeyName,RegistryKey);
+ RtlInitUnicodeString(&KeyName, RegistryKey);
InitializeObjectAttributes(&ObjectAttributes,
&KeyName,
OBJ_CASE_INSENSITIVE,
return FALSE;
}
- RtlInitUnicodeString(&ValueName,ValueNameStr);
+ RtlInitUnicodeString(&ValueName, ValueNameStr);
Status = ZwQueryValueKey(KeyHandle,
&ValueName,
KeyInfo,
KeyInfoSize,
&ReturnSize);
+
+ ZwClose(KeyHandle);
+
if (!NT_SUCCESS(Status))
{
- RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
- ZwClose(KeyHandle);
*nSize = ReturnSize;
- SetLastErrorByStatus(Status);
- return FALSE;
+ goto failed;
}
- if (lpBuffer && *nSize > (KeyInfo->DataLength / sizeof(WCHAR)))
+ if (KeyInfo->Type != REG_SZ)
{
- *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1;
- lpBuffer[*nSize] = 0;
+ Status = STATUS_UNSUCCESSFUL;
+ goto failed;
}
- else
+
+ if (!lpBuffer || *nSize < (KeyInfo->DataLength / sizeof(WCHAR)))
{
- RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
- ZwClose(KeyHandle);
*nSize = ReturnSize;
- SetLastErrorByStatus(STATUS_BUFFER_OVERFLOW);
- return FALSE;
+ Status = STATUS_BUFFER_OVERFLOW;
+ goto failed;
}
- RtlCopyMemory(lpBuffer, KeyInfo->Data, *nSize * sizeof(WCHAR));
+ *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1;
+ RtlCopyMemory(lpBuffer, KeyInfo->Data, KeyInfo->DataLength);
+ lpBuffer[*nSize] = 0;
RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
- ZwClose(KeyHandle);
return TRUE;
+
+failed:
+ RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
+ SetLastErrorByStatus(Status);
+ return FALSE;
}
/*