[WBEMPROX]
authorThomas Faber <thomas.faber@reactos.org>
Mon, 23 Feb 2015 15:25:29 +0000 (15:25 +0000)
committerThomas Faber <thomas.faber@reactos.org>
Mon, 23 Feb 2015 15:25:29 +0000 (15:25 +0000)
- Implement Win32_ComputerSystem.UserName
- Fix buffer size in get_computername
CORE-8678 #resolve

svn path=/trunk/; revision=66423

reactos/dll/win32/wbemprox/builtin.c
rostests/winetests/wbemprox/query.c

index 5fccbf4..ae2a5d3 100644 (file)
@@ -291,6 +291,8 @@ static const WCHAR prop_typeW[] =
     {'T','y','p','e',0};
 static const WCHAR prop_uniqueidW[] =
     {'U','n','i','q','u','e','I','d',0};
     {'T','y','p','e',0};
 static const WCHAR prop_uniqueidW[] =
     {'U','n','i','q','u','e','I','d',0};
+static const WCHAR prop_usernameW[] =
+    {'U','s','e','r','N','a','m','e',0};
 static const WCHAR prop_uuidW[] =
     {'U','U','I','D',0};
 static const WCHAR prop_varianttypeW[] =
 static const WCHAR prop_uuidW[] =
     {'U','U','I','D',0};
 static const WCHAR prop_varianttypeW[] =
@@ -343,7 +345,8 @@ static const struct column col_compsys[] =
     { prop_nameW,                 CIM_STRING|COL_FLAG_DYNAMIC },
     { prop_numlogicalprocessorsW, CIM_UINT32, VT_I4 },
     { prop_numprocessorsW,        CIM_UINT32, VT_I4 },
     { prop_nameW,                 CIM_STRING|COL_FLAG_DYNAMIC },
     { prop_numlogicalprocessorsW, CIM_UINT32, VT_I4 },
     { prop_numprocessorsW,        CIM_UINT32, VT_I4 },
-    { prop_totalphysicalmemoryW,  CIM_UINT64 }
+    { prop_totalphysicalmemoryW,  CIM_UINT64 },
+    { prop_usernameW,             CIM_STRING }
 };
 static const struct column col_compsysproduct[] =
 {
 };
 static const struct column col_compsysproduct[] =
 {
@@ -690,6 +693,7 @@ struct record_computersystem
     UINT32       num_logical_processors;
     UINT32       num_processors;
     UINT64       total_physical_memory;
     UINT32       num_logical_processors;
     UINT32       num_processors;
     UINT64       total_physical_memory;
+    const WCHAR *username;
 };
 struct record_computersystemproduct
 {
 };
 struct record_computersystemproduct
 {
@@ -1106,13 +1110,31 @@ static UINT64 get_total_physical_memory(void)
 static WCHAR *get_computername(void)
 {
     WCHAR *ret;
 static WCHAR *get_computername(void)
 {
     WCHAR *ret;
-    DWORD size = MAX_COMPUTERNAME_LENGTH;
+    DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
 
     if (!(ret = heap_alloc( size * sizeof(WCHAR) ))) return NULL;
     GetComputerNameW( ret, &size );
     return ret;
 }
 
 
     if (!(ret = heap_alloc( size * sizeof(WCHAR) ))) return NULL;
     GetComputerNameW( ret, &size );
     return ret;
 }
 
+static WCHAR *get_username(void)
+{
+    WCHAR *ret;
+    DWORD compsize, usersize;
+    DWORD size;
+
+    compsize = 0;
+    GetComputerNameW( NULL, &compsize );
+    usersize = 0;
+    GetUserNameW( NULL, &usersize );
+    size = compsize + usersize; /* two null terminators account for the \ */
+    if (!(ret = heap_alloc( size * sizeof(WCHAR) ))) return NULL;
+    GetComputerNameW( ret, &compsize );
+    ret[compsize] = '\\';
+    GetUserNameW( ret + compsize + 1, &usersize );
+    return ret;
+}
+
 static enum fill_status fill_compsys( struct table *table, const struct expr *cond )
 {
     struct record_computersystem *rec;
 static enum fill_status fill_compsys( struct table *table, const struct expr *cond )
 {
     struct record_computersystem *rec;
@@ -1131,6 +1153,7 @@ static enum fill_status fill_compsys( struct table *table, const struct expr *co
     rec->num_logical_processors = get_logical_processor_count( NULL );
     rec->num_processors         = get_processor_count();
     rec->total_physical_memory  = get_total_physical_memory();
     rec->num_logical_processors = get_logical_processor_count( NULL );
     rec->num_processors         = get_processor_count();
     rec->total_physical_memory  = get_total_physical_memory();
+    rec->username               = get_username();
     if (!match_row( table, row, cond, &status )) free_row_values( table, row );
     else row++;
 
     if (!match_row( table, row, cond, &status )) free_row_values( table, row );
     else row++;
 
index 5797e8f..742e20e 100644 (file)
@@ -340,6 +340,74 @@ static void test_Win32_Process( IWbemServices *services )
     IWbemClassObject_Release( out );
 }
 
     IWbemClassObject_Release( out );
 }
 
+static void test_Win32_ComputerSystem( IWbemServices *services )
+{
+    static const WCHAR nameW[] = {'N','a','m','e',0};
+    static const WCHAR usernameW[] = {'U','s','e','r','N','a','m','e',0};
+    static const WCHAR backslashW[] = {'\\',0};
+    static const WCHAR queryW[] =
+        {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','W','i','n','3','2','_',
+         'C','o','m','p','u','t','e','r','S','y','s','t','e','m',0};
+    BSTR wql = SysAllocString( wqlW ), query = SysAllocString( queryW );
+    IEnumWbemClassObject *result;
+    IWbemClassObject *service;
+    VARIANT value;
+    CIMTYPE type;
+    HRESULT hr;
+    WCHAR compname[MAX_COMPUTERNAME_LENGTH + 1];
+    WCHAR username[128];
+    DWORD len, count;
+
+    len = sizeof(compname) / sizeof(compname[0]);
+    if (!GetComputerNameW( compname, &len ))
+        compname[0] = 0;
+
+    lstrcpyW( username, compname );
+    lstrcatW( username, backslashW );
+    len = sizeof(username) / sizeof(username[0]) - lstrlenW( username );
+    if (!GetUserNameW( username + lstrlenW( username ), &len ))
+        username[0] = 0;
+
+    if (!compname[0] || !username[0])
+    {
+        skip( "Failed to get user or computer name\n" );
+        return;
+    }
+
+    hr = IWbemServices_ExecQuery( services, wql, query, 0, NULL, &result );
+    if (hr != S_OK)
+    {
+        win_skip( "Win32_ComputerSystem not available\n" );
+        return;
+    }
+
+    IEnumWbemClassObject_Next( result, 10000, 1, &service, &count );
+    ok( hr == S_OK, "got %08x\n", hr );
+
+    type = 0xdeadbeef;
+    VariantInit( &value );
+    hr = IWbemClassObject_Get( service, nameW, 0, &value, &type, NULL );
+    ok( hr == S_OK, "failed to get computer name %08x\n", hr );
+    ok( V_VT( &value ) == VT_BSTR, "unexpected variant type 0x%x\n", V_VT( &value ) );
+    ok( type == CIM_STRING, "unexpected type 0x%x\n", type );
+    ok( !lstrcmpiW( V_BSTR( &value ), compname ), "got %s, expected %s\n", wine_dbgstr_w(V_BSTR(&value)), wine_dbgstr_w(compname) );
+    VariantClear( &value );
+
+    type = 0xdeadbeef;
+    VariantInit( &value );
+    hr = IWbemClassObject_Get( service, usernameW, 0, &value, &type, NULL );
+    ok( hr == S_OK, "failed to get computer name %08x\n", hr );
+    ok( V_VT( &value ) == VT_BSTR, "unexpected variant type 0x%x\n", V_VT( &value ) );
+    ok( type == CIM_STRING, "unexpected type 0x%x\n", type );
+    ok( !lstrcmpiW( V_BSTR( &value ), username ), "got %s, expected %s\n", wine_dbgstr_w(V_BSTR(&value)), wine_dbgstr_w(username) );
+    VariantClear( &value );
+
+    IWbemClassObject_Release( service );
+    IEnumWbemClassObject_Release( result );
+    SysFreeString( query );
+    SysFreeString( wql );
+}
+
 static void test_StdRegProv( IWbemServices *services )
 {
     static const WCHAR enumkeyW[] = {'E','n','u','m','K','e','y',0};
 static void test_StdRegProv( IWbemServices *services )
 {
     static const WCHAR enumkeyW[] = {'E','n','u','m','K','e','y',0};
@@ -755,6 +823,7 @@ START_TEST(query)
     test_select( services );
     test_Win32_Process( services );
     test_Win32_Service( services );
     test_select( services );
     test_Win32_Process( services );
     test_Win32_Service( services );
+    test_Win32_ComputerSystem( services );
     test_StdRegProv( services );
     test_notification_query_async( services );
     test_query_async( services );
     test_StdRegProv( services );
     test_notification_query_async( services );
     test_query_async( services );