[WMIC] Sync with Wine Staging 3.17. CORE-15127
[reactos.git] / base / applications / cmdutils / wmic / main.c
index 6143177..f4a5bc7 100644 (file)
 
 #define COBJMACROS
 
-//#include <stdio.h>
-//#include "windows.h"
-//#include "ocidl.h"
-#include <initguid.h>
-//#include "objidl.h"
-#include <wbemcli.h>
+#include <stdio.h>
+#include "windows.h"
+#include "ocidl.h"
+#include "initguid.h"
+#include "objidl.h"
+#include "wbemcli.h"
 #include "wmic.h"
 
-#include <wine/debug.h>
-#include <wine/unicode.h>
+#include "wine/debug.h"
+#include "wine/unicode.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(wmic);
 
@@ -82,7 +82,7 @@ static const WCHAR *find_class( const WCHAR *alias )
 {
     unsigned int i;
 
-    for (i = 0; i < sizeof(alias_map)/sizeof(alias_map[0]); i++)
+    for (i = 0; i < ARRAY_SIZE(alias_map); i++)
     {
         if (!strcmpiW( alias, alias_map[i].alias )) return alias_map[i].class;
     }
@@ -158,11 +158,11 @@ static int output_message( int msg )
     static const WCHAR fmtW[] = {'%','s',0};
     WCHAR buffer[8192];
 
-    LoadStringW( GetModuleHandleW(NULL), msg, buffer, sizeof(buffer)/sizeof(WCHAR) );
+    LoadStringW( GetModuleHandleW(NULL), msg, buffer, ARRAY_SIZE(buffer));
     return output_string( fmtW, buffer );
 }
 
-static int query_prop( const WCHAR *alias, const WCHAR *propname )
+static int query_prop( const WCHAR *class, const WCHAR *propname )
 {
     static const WCHAR select_allW[] = {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',0};
     static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
@@ -175,18 +175,12 @@ static int query_prop( const WCHAR *alias, const WCHAR *propname )
     IEnumWbemClassObject *result = NULL;
     LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
     BSTR path = NULL, wql = NULL, query = NULL;
-    const WCHAR *class;
     WCHAR *prop = NULL;
     BOOL first = TRUE;
     int len, ret = -1;
 
-    WINE_TRACE("%s, %s\n", debugstr_w(alias), debugstr_w(propname));
+    WINE_TRACE("%s, %s\n", debugstr_w(class), debugstr_w(propname));
 
-    if (!(class = find_class( alias )))
-    {
-        output_message( STRING_ALIAS_NOT_FOUND );
-        return -1;
-    }
     CoInitialize( NULL );
     CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
                           RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
@@ -199,7 +193,7 @@ static int query_prop( const WCHAR *alias, const WCHAR *propname )
     hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
     if (hr != S_OK) goto done;
 
-    len = strlenW( class ) + sizeof(select_allW) / sizeof(select_allW[0]);
+    len = strlenW( class ) + ARRAY_SIZE(select_allW);
     if (!(query = SysAllocStringLen( NULL, len ))) goto done;
     strcpyW( query, select_allW );
     strcatW( query, class );
@@ -229,6 +223,7 @@ static int query_prop( const WCHAR *alias, const WCHAR *propname )
         }
         if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
         {
+            VariantChangeType( &v, &v, 0, VT_BSTR );
             output_string( fmtW, V_BSTR( &v ) );
             VariantClear( &v );
         }
@@ -252,11 +247,64 @@ done:
 int wmain(int argc, WCHAR *argv[])
 {
     static const WCHAR getW[] = {'g','e','t',0};
+    static const WCHAR quitW[] = {'q','u','i','t',0};
+    static const WCHAR exitW[] = {'e','x','i','t',0};
+    static const WCHAR pathW[] = {'p','a','t','h',0};
+    static const WCHAR classW[] = {'c','l','a','s','s',0};
+    static const WCHAR contextW[] = {'c','o','n','t','e','x','t',0};
+    const WCHAR *class, *value;
+    int i;
+
+    for (i = 1; i < argc && argv[i][0] == '/'; i++)
+        WINE_FIXME( "command line switch %s not supported\n", debugstr_w(argv[i]) );
+
+    if (i >= argc)
+        goto not_supported;
+
+    if (!strcmpiW( argv[i], quitW ) ||
+        !strcmpiW( argv[i], exitW ))
+    {
+        return 0;
+    }
 
-    if (argc != 4 || strcmpiW( argv[2], getW ))
+    if (!strcmpiW( argv[i], classW) ||
+        !strcmpiW( argv[i], contextW ))
     {
-        output_message( STRING_CMDLINE_NOT_SUPPORTED );
-        return -1;
+        WINE_FIXME( "command %s not supported\n", debugstr_w(argv[i]) );
+        goto not_supported;
+    }
+
+    if (!strcmpiW( argv[i], pathW ))
+    {
+        if (++i >= argc)
+        {
+            output_message( STRING_INVALID_PATH );
+            return 1;
+        }
+        class = argv[i];
     }
-    return query_prop( argv[1], argv[3] );
+    else
+    {
+        class = find_class( argv[i] );
+        if (!class)
+        {
+            output_message( STRING_ALIAS_NOT_FOUND );
+            return 1;
+        }
+    }
+
+    if (++i >= argc)
+        goto not_supported;
+
+    if (!strcmpiW( argv[i], getW ))
+    {
+        if (++i >= argc)
+            goto not_supported;
+        value = argv[i];
+        return query_prop( class, value );
+    }
+
+not_supported:
+    output_message( STRING_CMDLINE_NOT_SUPPORTED );
+    return 1;
 }