Commit 49c5bd03 authored by Alexandre Goujon's avatar Alexandre Goujon Committed by Alexandre Julliard

kernel32: Fix GetComputerName errors.

parent 53aef28e
...@@ -316,23 +316,33 @@ BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size) ...@@ -316,23 +316,33 @@ BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
DWORD len = sizeof( buf ); DWORD len = sizeof( buf );
LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )); LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
NTSTATUS st = STATUS_INVALID_PARAMETER; NTSTATUS st = STATUS_INVALID_PARAMETER;
DWORD err = ERROR_SUCCESS;
TRACE ("%p %p\n", name, size); TRACE ("%p %p\n", name, size);
_init_attr ( &attr, &nameW ); _init_attr ( &attr, &nameW );
RtlInitUnicodeString( &nameW, ComputerW ); RtlInitUnicodeString( &nameW, ComputerW );
if ( ( st = NtOpenKey( &hkey, KEY_READ, &attr ) ) != STATUS_SUCCESS ) if ( ( st = NtOpenKey( &hkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
{
err = RtlNtStatusToDosError ( st );
goto out; goto out;
}
attr.RootDirectory = hkey; attr.RootDirectory = hkey;
RtlInitUnicodeString( &nameW, ActiveComputerNameW ); RtlInitUnicodeString( &nameW, ActiveComputerNameW );
if ( ( st = NtOpenKey( &hsubkey, KEY_READ, &attr ) ) != STATUS_SUCCESS ) if ( ( st = NtOpenKey( &hsubkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
{
err = RtlNtStatusToDosError ( st );
goto out; goto out;
}
RtlInitUnicodeString( &nameW, ComputerNameW ); RtlInitUnicodeString( &nameW, ComputerNameW );
if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) ) if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
!= STATUS_SUCCESS ) != STATUS_SUCCESS )
{
err = RtlNtStatusToDosError ( st );
goto out; goto out;
}
len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1; len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len); TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len);
...@@ -340,25 +350,24 @@ BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size) ...@@ -340,25 +350,24 @@ BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
if ( *size < len + 1 ) if ( *size < len + 1 )
{ {
*size = len + 1; *size = len + 1;
st = STATUS_MORE_ENTRIES; err = ERROR_BUFFER_OVERFLOW;
} }
else else
{ {
memcpy ( name, theName, len * sizeof (WCHAR) ); memcpy ( name, theName, len * sizeof (WCHAR) );
name[len] = 0; name[len] = 0;
*size = len; *size = len;
st = STATUS_SUCCESS;
} }
out: out:
NtClose ( hsubkey ); NtClose ( hsubkey );
NtClose ( hkey ); NtClose ( hkey );
if ( st == STATUS_SUCCESS ) if ( err == ERROR_SUCCESS )
return TRUE; return TRUE;
else else
{ {
SetLastError ( RtlNtStatusToDosError ( st ) ); SetLastError ( err );
WARN ( "Status %u reading computer name from registry\n", st ); WARN ( "Status %u reading computer name from registry\n", st );
return FALSE; return FALSE;
} }
...@@ -383,7 +392,7 @@ BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size) ...@@ -383,7 +392,7 @@ BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
if ( *size < len ) if ( *size < len )
{ {
*size = len; *size = len;
SetLastError( ERROR_MORE_DATA ); SetLastError( ERROR_BUFFER_OVERFLOW );
ret = FALSE; ret = FALSE;
} }
else else
...@@ -415,7 +424,11 @@ BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD si ...@@ -415,7 +424,11 @@ BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD si
{ {
case ComputerNameNetBIOS: case ComputerNameNetBIOS:
case ComputerNamePhysicalNetBIOS: case ComputerNamePhysicalNetBIOS:
return GetComputerNameA (name, size); ret = GetComputerNameA (name, size);
if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
SetLastError( ERROR_MORE_DATA );
return ret;
case ComputerNameDnsHostname: case ComputerNameDnsHostname:
case ComputerNamePhysicalDnsHostname: case ComputerNamePhysicalDnsHostname:
ret = dns_hostname (buf, &len); ret = dns_hostname (buf, &len);
...@@ -468,7 +481,10 @@ BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD ...@@ -468,7 +481,10 @@ BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD
{ {
case ComputerNameNetBIOS: case ComputerNameNetBIOS:
case ComputerNamePhysicalNetBIOS: case ComputerNamePhysicalNetBIOS:
return GetComputerNameW (name, size); ret = GetComputerNameW (name, size);
if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
SetLastError( ERROR_MORE_DATA );
return ret;
case ComputerNameDnsHostname: case ComputerNameDnsHostname:
case ComputerNamePhysicalDnsHostname: case ComputerNamePhysicalDnsHostname:
ret = dns_hostname (buf, &len); ret = dns_hostname (buf, &len);
......
...@@ -330,7 +330,6 @@ static void test_GetComputerName(void) ...@@ -330,7 +330,6 @@ static void test_GetComputerName(void)
size = 0; size = 0;
ret = GetComputerNameA((LPSTR)0xdeadbeef, &size); ret = GetComputerNameA((LPSTR)0xdeadbeef, &size);
error = GetLastError(); error = GetLastError();
todo_wine
ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error); ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
/* Only Vista returns the computer name length as documented in the MSDN */ /* Only Vista returns the computer name length as documented in the MSDN */
...@@ -362,7 +361,6 @@ static void test_GetComputerName(void) ...@@ -362,7 +361,6 @@ static void test_GetComputerName(void)
win_skip("GetComputerNameW is not implemented\n"); win_skip("GetComputerNameW is not implemented\n");
else else
{ {
todo_wine
ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error); ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
size++; /* nul terminating character */ size++; /* nul terminating character */
nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0])); nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment