- Formatting fix. No code change

svn path=/trunk/; revision=38688
This commit is contained in:
Dmitry Chapyshev 2009-01-10 19:09:37 +00:00
parent faff43dadc
commit a7f8cd9375

View file

@ -34,10 +34,13 @@
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey, static
BOOL
GetComputerNameFromRegistry(LPWSTR RegistryKey,
LPWSTR ValueNameStr, LPWSTR ValueNameStr,
LPWSTR lpBuffer, LPWSTR lpBuffer,
LPDWORD nSize ) { LPDWORD nSize )
{
PKEY_VALUE_PARTIAL_INFORMATION KeyInfo; PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
OBJECT_ATTRIBUTES ObjectAttributes; OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING KeyName; UNICODE_STRING KeyName;
@ -47,13 +50,14 @@ static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey,
ULONG ReturnSize; ULONG ReturnSize;
NTSTATUS Status; NTSTATUS Status;
RtlInitUnicodeString (&KeyName,RegistryKey); RtlInitUnicodeString(&KeyName,RegistryKey);
InitializeObjectAttributes (&ObjectAttributes, InitializeObjectAttributes(&ObjectAttributes,
&KeyName, &KeyName,
OBJ_CASE_INSENSITIVE, OBJ_CASE_INSENSITIVE,
NULL, NULL,
NULL); NULL);
Status = ZwOpenKey (&KeyHandle,
Status = ZwOpenKey(&KeyHandle,
KEY_READ, KEY_READ,
&ObjectAttributes); &ObjectAttributes);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
@ -62,21 +66,18 @@ static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey,
return FALSE; return FALSE;
} }
KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + *nSize * sizeof(WCHAR);
*nSize * sizeof(WCHAR); KeyInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, KeyInfoSize);
KeyInfo = RtlAllocateHeap (RtlGetProcessHeap (),
0,
KeyInfoSize);
if (KeyInfo == NULL) if (KeyInfo == NULL)
{ {
ZwClose (KeyHandle); ZwClose(KeyHandle);
SetLastError (ERROR_OUTOFMEMORY); SetLastError(ERROR_OUTOFMEMORY);
return FALSE; return FALSE;
} }
RtlInitUnicodeString (&ValueName,ValueNameStr); RtlInitUnicodeString(&ValueName,ValueNameStr);
Status = ZwQueryValueKey (KeyHandle, Status = ZwQueryValueKey(KeyHandle,
&ValueName, &ValueName,
KeyValuePartialInformation, KeyValuePartialInformation,
KeyInfo, KeyInfo,
@ -84,28 +85,23 @@ static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey,
&ReturnSize); &ReturnSize);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
RtlFreeHeap (RtlGetProcessHeap (), RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
0, ZwClose(KeyHandle);
KeyInfo);
ZwClose (KeyHandle);
*nSize = ReturnSize; *nSize = ReturnSize;
SetLastErrorByStatus (Status); SetLastErrorByStatus(Status);
return FALSE; return FALSE;
} }
if( *nSize > (KeyInfo->DataLength / sizeof(WCHAR)) ) { if (*nSize > (KeyInfo->DataLength / sizeof(WCHAR)))
{
*nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1; *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1;
lpBuffer[*nSize] = 0; lpBuffer[*nSize] = 0;
} }
RtlCopyMemory (lpBuffer, RtlCopyMemory(lpBuffer, KeyInfo->Data, *nSize * sizeof(WCHAR));
KeyInfo->Data,
*nSize * sizeof(WCHAR));
RtlFreeHeap (RtlGetProcessHeap (), RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
0, ZwClose(KeyHandle);
KeyInfo);
ZwClose (KeyHandle);
return TRUE; return TRUE;
} }
@ -113,12 +109,11 @@ static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey,
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
GetComputerNameExW ( WINAPI
COMPUTER_NAME_FORMAT NameType, GetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
LPWSTR lpBuffer, LPWSTR lpBuffer,
LPDWORD nSize LPDWORD nSize)
)
{ {
UNICODE_STRING ResultString; UNICODE_STRING ResultString;
UNICODE_STRING DomainPart; UNICODE_STRING DomainPart;
@ -127,22 +122,21 @@ GetComputerNameExW (
BOOL ret = TRUE; BOOL ret = TRUE;
DWORD HostSize; DWORD HostSize;
switch( NameType ) { switch (NameType)
{
case ComputerNameNetBIOS: case ComputerNameNetBIOS:
return GetComputerNameFromRegistry return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Control\\ComputerName\\ComputerName", L"\\Control\\ComputerName\\ComputerName",
L"ComputerName", L"ComputerName",
lpBuffer, lpBuffer,
nSize ); nSize);
case ComputerNameDnsDomain: case ComputerNameDnsDomain:
return GetComputerNameFromRegistry return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Services\\Tcpip\\Parameters", L"\\Services\\Tcpip\\Parameters",
L"Domain", L"Domain",
lpBuffer, lpBuffer,
nSize ); nSize);
case ComputerNameDnsFullyQualified: case ComputerNameDnsFullyQualified:
ResultString.Length = 0; ResultString.Length = 0;
@ -151,6 +145,7 @@ GetComputerNameExW (
RtlZeroMemory(QueryTable, sizeof(QueryTable)); RtlZeroMemory(QueryTable, sizeof(QueryTable));
RtlInitUnicodeString(&DomainPart, NULL); RtlInitUnicodeString(&DomainPart, NULL);
QueryTable[0].Name = L"HostName"; QueryTable[0].Name = L"HostName";
QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
QueryTable[0].EntryContext = &DomainPart; QueryTable[0].EntryContext = &DomainPart;
@ -159,14 +154,20 @@ GetComputerNameExW (
L"\\Registry\\Machine\\System" L"\\Registry\\Machine\\System"
L"\\CurrentControlSet\\Services\\Tcpip" L"\\CurrentControlSet\\Services\\Tcpip"
L"\\Parameters", L"\\Parameters",
QueryTable, NULL, NULL); QueryTable,
NULL,
NULL);
if( NT_SUCCESS(Status) ) { if (NT_SUCCESS(Status))
{
Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart);
HostSize = DomainPart.Length; HostSize = DomainPart.Length;
if (!NT_SUCCESS(Status)) {
if (!NT_SUCCESS(Status))
{
ret = FALSE; ret = FALSE;
} }
RtlAppendUnicodeToString(&ResultString, L"."); RtlAppendUnicodeToString(&ResultString, L".");
RtlFreeUnicodeString(&DomainPart); RtlFreeUnicodeString(&DomainPart);
@ -179,11 +180,15 @@ GetComputerNameExW (
L"\\Registry\\Machine\\System" L"\\Registry\\Machine\\System"
L"\\CurrentControlSet\\Services\\Tcpip" L"\\CurrentControlSet\\Services\\Tcpip"
L"\\Parameters", L"\\Parameters",
QueryTable, NULL, NULL); QueryTable,
NULL,
NULL);
if( NT_SUCCESS(Status) ) { if (NT_SUCCESS(Status))
{
Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart);
if ( (!NT_SUCCESS(Status)) || (!ret)) { if ((!NT_SUCCESS(Status)) || (!ret))
{
*nSize = HostSize + DomainPart.Length; *nSize = HostSize + DomainPart.Length;
SetLastError(ERROR_MORE_DATA); SetLastError(ERROR_MORE_DATA);
RtlFreeUnicodeString(&DomainPart); RtlFreeUnicodeString(&DomainPart);
@ -197,31 +202,34 @@ GetComputerNameExW (
return FALSE; return FALSE;
case ComputerNameDnsHostname: case ComputerNameDnsHostname:
return GetComputerNameFromRegistry return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Services\\Tcpip\\Parameters", L"\\Services\\Tcpip\\Parameters",
L"Hostname", L"Hostname",
lpBuffer, lpBuffer,
nSize ); nSize);
case ComputerNamePhysicalDnsDomain: case ComputerNamePhysicalDnsDomain:
return GetComputerNameFromRegistry return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Services\\Tcpip\\Parameters", L"\\Services\\Tcpip\\Parameters",
L"Domain", L"Domain",
lpBuffer, lpBuffer,
nSize ); nSize);
/* XXX Redo these */ /* XXX Redo these */
case ComputerNamePhysicalDnsFullyQualified: case ComputerNamePhysicalDnsFullyQualified:
return GetComputerNameExW( ComputerNameDnsFullyQualified, return GetComputerNameExW(ComputerNameDnsFullyQualified,
lpBuffer, nSize ); lpBuffer,
nSize);
case ComputerNamePhysicalDnsHostname: case ComputerNamePhysicalDnsHostname:
return GetComputerNameExW( ComputerNameDnsHostname, return GetComputerNameExW(ComputerNameDnsHostname,
lpBuffer, nSize ); lpBuffer,
nSize);
case ComputerNamePhysicalNetBIOS: case ComputerNamePhysicalNetBIOS:
return GetComputerNameExW( ComputerNameNetBIOS, return GetComputerNameExW(ComputerNameNetBIOS,
lpBuffer, nSize ); lpBuffer,
nSize);
case ComputerNameMax: case ComputerNameMax:
return FALSE; return FALSE;
@ -235,18 +243,17 @@ GetComputerNameExW (
*/ */
BOOL BOOL
WINAPI WINAPI
GetComputerNameExA ( GetComputerNameExA(COMPUTER_NAME_FORMAT NameType,
COMPUTER_NAME_FORMAT NameType,
LPSTR lpBuffer, LPSTR lpBuffer,
LPDWORD nSize LPDWORD nSize)
)
{ {
UNICODE_STRING UnicodeString; UNICODE_STRING UnicodeString;
ANSI_STRING AnsiString; ANSI_STRING AnsiString;
BOOL Result; BOOL Result;
PWCHAR TempBuffer = RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize * sizeof(WCHAR) ); PWCHAR TempBuffer = RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize * sizeof(WCHAR) );
if( !TempBuffer ) { if (!TempBuffer)
{
return ERROR_OUTOFMEMORY; return ERROR_OUTOFMEMORY;
} }
@ -254,19 +261,20 @@ GetComputerNameExA (
AnsiString.Length = 0; AnsiString.Length = 0;
AnsiString.Buffer = lpBuffer; 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.MaximumLength = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR);
UnicodeString.Length = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR); UnicodeString.Length = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR);
UnicodeString.Buffer = TempBuffer; UnicodeString.Buffer = TempBuffer;
RtlUnicodeStringToAnsiString (&AnsiString, RtlUnicodeStringToAnsiString(&AnsiString,
&UnicodeString, &UnicodeString,
FALSE); FALSE);
} }
RtlFreeHeap( RtlGetProcessHeap(), 0, TempBuffer ); RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer);
return Result; return Result;
} }
@ -274,31 +282,31 @@ GetComputerNameExA (
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
GetComputerNameA (LPSTR lpBuffer, WINAPI
LPDWORD lpnSize) GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
{ {
return GetComputerNameExA( ComputerNameNetBIOS, lpBuffer, lpnSize ); return GetComputerNameExA(ComputerNameNetBIOS, lpBuffer, lpnSize);
} }
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
GetComputerNameW (LPWSTR lpBuffer, WINAPI
LPDWORD lpnSize) GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
{ {
return GetComputerNameExW( ComputerNameNetBIOS, lpBuffer, lpnSize ); return GetComputerNameExW(ComputerNameNetBIOS, lpBuffer, lpnSize);
} }
/* /*
* @implemented * @implemented
*/ */
static BOOL static
IsValidComputerName ( BOOL
COMPUTER_NAME_FORMAT NameType, IsValidComputerName(COMPUTER_NAME_FORMAT NameType,
LPCWSTR lpComputerName) LPCWSTR lpComputerName)
{ {
PWCHAR p; PWCHAR p;
@ -308,6 +316,7 @@ IsValidComputerName (
Length = 0; Length = 0;
p = (PWCHAR)lpComputerName; p = (PWCHAR)lpComputerName;
while (*p != 0) while (*p != 0)
{ {
if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' || if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' ||
@ -320,16 +329,16 @@ IsValidComputerName (
p++; p++;
} }
if (Length == 0 || if (Length == 0 || Length > MAX_COMPUTERNAME_LENGTH)
Length > MAX_COMPUTERNAME_LENGTH)
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
static BOOL SetComputerNameToRegistry( static
LPCWSTR RegistryKey, BOOL
SetComputerNameToRegistry(LPCWSTR RegistryKey,
LPCWSTR ValueNameStr, LPCWSTR ValueNameStr,
LPCWSTR lpBuffer) LPCWSTR lpBuffer)
{ {
@ -339,25 +348,25 @@ static BOOL SetComputerNameToRegistry(
HANDLE KeyHandle; HANDLE KeyHandle;
NTSTATUS Status; NTSTATUS Status;
RtlInitUnicodeString (&KeyName, RegistryKey); RtlInitUnicodeString(&KeyName, RegistryKey);
InitializeObjectAttributes (&ObjectAttributes, InitializeObjectAttributes(&ObjectAttributes,
&KeyName, &KeyName,
OBJ_CASE_INSENSITIVE, OBJ_CASE_INSENSITIVE,
NULL, NULL,
NULL ); NULL);
Status = NtOpenKey (&KeyHandle, Status = NtOpenKey(&KeyHandle,
KEY_WRITE, KEY_WRITE,
&ObjectAttributes); &ObjectAttributes);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
SetLastErrorByStatus (Status); SetLastErrorByStatus(Status);
return FALSE; return FALSE;
} }
RtlInitUnicodeString (&ValueName, ValueNameStr); RtlInitUnicodeString(&ValueName, ValueNameStr);
Status = NtSetValueKey (KeyHandle, Status = NtSetValueKey(KeyHandle,
&ValueName, &ValueName,
0, 0,
REG_SZ, REG_SZ,
@ -365,13 +374,13 @@ static BOOL SetComputerNameToRegistry(
(wcslen (lpBuffer) + 1) * sizeof(WCHAR)); (wcslen (lpBuffer) + 1) * sizeof(WCHAR));
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
ZwClose (KeyHandle); ZwClose(KeyHandle);
SetLastErrorByStatus (Status); SetLastErrorByStatus(Status);
return FALSE; return FALSE;
} }
NtFlushKey (KeyHandle); NtFlushKey(KeyHandle);
ZwClose (KeyHandle); ZwClose(KeyHandle);
return TRUE; return TRUE;
} }
@ -380,40 +389,41 @@ static BOOL SetComputerNameToRegistry(
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
SetComputerNameA (LPCSTR lpComputerName) WINAPI
SetComputerNameA(LPCSTR lpComputerName)
{ {
return SetComputerNameExA( ComputerNamePhysicalNetBIOS, lpComputerName ); return SetComputerNameExA(ComputerNamePhysicalNetBIOS, lpComputerName);
} }
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
SetComputerNameW (LPCWSTR lpComputerName) WINAPI
SetComputerNameW(LPCWSTR lpComputerName)
{ {
return SetComputerNameExW( ComputerNamePhysicalNetBIOS, lpComputerName ); return SetComputerNameExW(ComputerNamePhysicalNetBIOS, lpComputerName);
} }
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
SetComputerNameExA ( WINAPI
COMPUTER_NAME_FORMAT NameType, SetComputerNameExA(COMPUTER_NAME_FORMAT NameType,
LPCSTR lpBuffer) LPCSTR lpBuffer)
{ {
UNICODE_STRING Buffer; UNICODE_STRING Buffer;
BOOL bResult; BOOL bResult;
RtlCreateUnicodeStringFromAsciiz (&Buffer, RtlCreateUnicodeStringFromAsciiz(&Buffer, (LPSTR)lpBuffer);
(LPSTR)lpBuffer);
bResult = SetComputerNameExW (NameType, Buffer.Buffer); bResult = SetComputerNameExW(NameType, Buffer.Buffer);
RtlFreeUnicodeString (&Buffer); RtlFreeUnicodeString(&Buffer);
return bResult; return bResult;
} }
@ -422,38 +432,36 @@ SetComputerNameExA (
/* /*
* @implemented * @implemented
*/ */
BOOL WINAPI BOOL
SetComputerNameExW ( WINAPI
COMPUTER_NAME_FORMAT NameType, SetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
LPCWSTR lpBuffer) LPCWSTR lpBuffer)
{ {
if (!IsValidComputerName (NameType, lpBuffer)) if (!IsValidComputerName(NameType, lpBuffer))
{ {
SetLastError (ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
switch( NameType ) { switch( NameType )
{
case ComputerNamePhysicalDnsDomain: case ComputerNamePhysicalDnsDomain:
return SetComputerNameToRegistry return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Services\\Tcpip\\Parameters", L"\\Services\\Tcpip\\Parameters",
L"Domain", L"Domain",
lpBuffer ); lpBuffer);
case ComputerNamePhysicalDnsHostname: case ComputerNamePhysicalDnsHostname:
return SetComputerNameToRegistry return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Services\\Tcpip\\Parameters", L"\\Services\\Tcpip\\Parameters",
L"Hostname", L"Hostname",
lpBuffer ); lpBuffer);
case ComputerNamePhysicalNetBIOS: case ComputerNamePhysicalNetBIOS:
return SetComputerNameToRegistry return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
( L"\\Registry\\Machine\\System\\CurrentControlSet"
L"\\Control\\ComputerName\\ComputerName", L"\\Control\\ComputerName\\ComputerName",
L"ComputerName", L"ComputerName",
lpBuffer ); lpBuffer);
default: default:
SetLastError (ERROR_INVALID_PARAMETER); SetLastError (ERROR_INVALID_PARAMETER);
@ -491,7 +499,7 @@ DnsHostnameToComputerNameA(LPCSTR Hostname,
if (!ComputerName) return FALSE; if (!ComputerName) return FALSE;
memcpy( ComputerName, Hostname, len ); memcpy(ComputerName, Hostname, len);
ComputerName[len + 1] = 0; ComputerName[len + 1] = 0;
return TRUE; return TRUE;
} }
@ -502,11 +510,9 @@ DnsHostnameToComputerNameA(LPCSTR Hostname,
*/ */
BOOL BOOL
WINAPI WINAPI
DnsHostnameToComputerNameW ( DnsHostnameToComputerNameW(LPCWSTR hostname,
LPCWSTR hostname,
LPWSTR computername, LPWSTR computername,
LPDWORD size LPDWORD size)
)
{ {
DWORD len; DWORD len;
@ -525,7 +531,7 @@ DnsHostnameToComputerNameW (
} }
if (!computername) return FALSE; if (!computername) return FALSE;
memcpy( computername, hostname, len * sizeof(WCHAR) ); memcpy(computername, hostname, len * sizeof(WCHAR));
computername[len + 1] = 0; computername[len + 1] = 0;
return TRUE; return TRUE;
} }