winmm: Use wide-char string literals.
[wine.git] / programs / wmic / main.c
blob1372c0986e99909df5332d68da409b7708dabc42
1 /*
2 * Copyright 2010 Louis Lenders
3 * Copyright 2012 Hans Leidekker for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define COBJMACROS
22 #include <stdio.h>
23 #include "windows.h"
24 #include "ocidl.h"
25 #include "initguid.h"
26 #include "objidl.h"
27 #include "wbemcli.h"
28 #include "wmic.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wmic);
34 static const struct
36 const WCHAR *alias;
37 const WCHAR *class;
39 alias_map[] =
41 { L"bios", L"Win32_BIOS" },
42 { L"computersystem", L"Win32_ComputerSystem" },
43 { L"cpu", L"Win32_Processor" },
44 { L"LogicalDisk", L"Win32_LogicalDisk" },
45 { L"nic", L"Win32_NetworkAdapter" },
46 { L"os", L"Win32_OperatingSystem" },
47 { L"process", L"Win32_Process" }
50 static const WCHAR *find_class( const WCHAR *alias )
52 unsigned int i;
54 for (i = 0; i < ARRAY_SIZE(alias_map); i++)
56 if (!wcsicmp( alias, alias_map[i].alias )) return alias_map[i].class;
58 return NULL;
61 static inline WCHAR *strdupW( const WCHAR *src )
63 WCHAR *dst;
64 if (!src) return NULL;
65 if (!(dst = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( src ) + 1) * sizeof(WCHAR) ))) return NULL;
66 lstrcpyW( dst, src );
67 return dst;
70 static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
72 SAFEARRAY *sa;
73 WCHAR *ret = NULL;
74 LONG i, last_index = 0;
75 BSTR str;
77 if (IWbemClassObject_GetNames( class, NULL, WBEM_FLAG_ALWAYS, NULL, &sa ) != S_OK) return NULL;
79 SafeArrayGetUBound( sa, 1, &last_index );
80 for (i = 0; i <= last_index; i++)
82 SafeArrayGetElement( sa, &i, &str );
83 if (!wcsicmp( str, prop ))
85 ret = strdupW( str );
86 break;
89 SafeArrayDestroy( sa );
90 return ret;
93 static int WINAPIV output_string( HANDLE handle, const WCHAR *msg, ... )
95 __ms_va_list va_args;
96 int len;
97 DWORD count;
98 WCHAR buffer[8192];
100 __ms_va_start( va_args, msg );
101 len = vswprintf( buffer, ARRAY_SIZE(buffer), msg, va_args );
102 __ms_va_end( va_args );
104 if (!WriteConsoleW( handle, buffer, len, &count, NULL ))
105 WriteFile( handle, buffer, len * sizeof(WCHAR), &count, FALSE );
107 return count;
110 static int output_error( int msg )
112 WCHAR buffer[8192];
114 LoadStringW( GetModuleHandleW(NULL), msg, buffer, ARRAY_SIZE(buffer));
115 return output_string( GetStdHandle(STD_ERROR_HANDLE), L"%s", buffer );
118 static int output_header( const WCHAR *prop, ULONG column_width )
120 static const WCHAR bomW[] = {0xfeff};
121 int len;
122 DWORD count;
123 WCHAR buffer[8192];
125 len = swprintf( buffer, ARRAY_SIZE(buffer), L"%-*s\r\n", column_width, prop );
127 if (!WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, &count, NULL )) /* redirected */
129 WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), bomW, sizeof(bomW), &count, FALSE );
130 WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len * sizeof(WCHAR), &count, FALSE );
131 count += sizeof(bomW);
134 return count;
137 static int output_line( const WCHAR *str, ULONG column_width )
139 return output_string( GetStdHandle(STD_OUTPUT_HANDLE), L"%-*s\r\n", column_width, str );
142 static int query_prop( const WCHAR *class, const WCHAR *propname )
144 HRESULT hr;
145 IWbemLocator *locator = NULL;
146 IWbemServices *services = NULL;
147 IEnumWbemClassObject *result = NULL;
148 LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
149 BSTR path = NULL, wql = NULL, query = NULL;
150 WCHAR *prop = NULL;
151 BOOL first = TRUE;
152 int len, ret = -1;
153 IWbemClassObject *obj;
154 ULONG count, width = 0;
155 VARIANT v;
157 WINE_TRACE("%s, %s\n", debugstr_w(class), debugstr_w(propname));
159 CoInitialize( NULL );
160 CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
161 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
163 hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
164 (void **)&locator );
165 if (hr != S_OK) goto done;
167 if (!(path = SysAllocString(L"ROOT\\CIMV2" ))) goto done;
168 hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
169 if (hr != S_OK) goto done;
171 len = lstrlenW( class ) + ARRAY_SIZE(L"SELECT * FROM ");
172 if (!(query = SysAllocStringLen( NULL, len ))) goto done;
173 lstrcpyW( query, L"SELECT * FROM " );
174 lstrcatW( query, class );
176 if (!(wql = SysAllocString(L"WQL" ))) goto done;
177 hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, &result );
178 if (hr != S_OK) goto done;
180 for (;;) /* get column width */
182 IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
183 if (!count) break;
185 if (!prop && !(prop = find_prop( obj, propname )))
187 output_error( STRING_INVALID_QUERY );
188 goto done;
190 if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
192 VariantChangeType( &v, &v, 0, VT_BSTR );
193 width = max( lstrlenW( V_BSTR( &v ) ), width );
194 VariantClear( &v );
196 IWbemClassObject_Release( obj );
198 width += 2;
200 IEnumWbemClassObject_Reset( result );
201 for (;;)
203 IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
204 if (!count) break;
206 if (first)
208 output_header( prop, width );
209 first = FALSE;
211 if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
213 VariantChangeType( &v, &v, 0, VT_BSTR );
214 output_line( V_BSTR( &v ), width );
215 VariantClear( &v );
217 IWbemClassObject_Release( obj );
219 ret = 0;
221 done:
222 if (result) IEnumWbemClassObject_Release( result );
223 if (services) IWbemServices_Release( services );
224 if (locator) IWbemLocator_Release( locator );
225 SysFreeString( path );
226 SysFreeString( query );
227 SysFreeString( wql );
228 HeapFree( GetProcessHeap(), 0, prop );
229 CoUninitialize();
230 return ret;
233 int __cdecl wmain(int argc, WCHAR *argv[])
235 const WCHAR *class, *value;
236 int i;
238 for (i = 1; i < argc && argv[i][0] == '/'; i++)
239 WINE_FIXME( "command line switch %s not supported\n", debugstr_w(argv[i]) );
241 if (i >= argc)
242 goto not_supported;
244 if (!wcsicmp( argv[i], L"quit" ) || !wcsicmp( argv[i], L"exit" ))
246 return 0;
249 if (!wcsicmp( argv[i], L"class") || !wcsicmp( argv[i], L"context" ))
251 WINE_FIXME( "command %s not supported\n", debugstr_w(argv[i]) );
252 goto not_supported;
255 if (!wcsicmp( argv[i], L"path" ))
257 if (++i >= argc)
259 output_error( STRING_INVALID_PATH );
260 return 1;
262 class = argv[i];
264 else
266 class = find_class( argv[i] );
267 if (!class)
269 output_error( STRING_ALIAS_NOT_FOUND );
270 return 1;
274 if (++i >= argc)
275 goto not_supported;
277 if (!wcsicmp( argv[i], L"get" ))
279 if (++i >= argc)
280 goto not_supported;
281 value = argv[i];
282 return query_prop( class, value );
285 not_supported:
286 output_error( STRING_CMDLINE_NOT_SUPPORTED );
287 return 1;