[WMIC]
[reactos.git] / reactos / base / applications / cmdutils / wmic / wmic.c
1 /*
2 * Copyright 2010 Louis Lenders
3 * Copyright 2012 Hans Leidekker for CodeWeavers
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #define COBJMACROS
21
22 #include <stdio.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <wincon.h>
26 #include <ocidl.h>
27 #include <initguid.h>
28 #include "objidl.h"
29 #include "wbemcli.h"
30 #include <wine/debug.h>
31 #include <wine/unicode.h>
32
33 #include "wmic.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(wmic);
36
37 static const WCHAR biosW[] =
38 {'b','i','o','s',0};
39 static const WCHAR computersystemW[] =
40 {'c','o','m','p','u','t','e','r','s','y','s','t','e','m',0};
41 static const WCHAR cpuW[] =
42 {'c','p','u',0};
43 static const WCHAR logicaldiskW[] =
44 {'L','o','g','i','c','a','l','D','i','s','k',0};
45 static const WCHAR nicW[] =
46 {'n','i','c',0};
47 static const WCHAR osW[] =
48 {'o','s',0};
49 static const WCHAR processW[] =
50 {'p','r','o','c','e','s','s',0};
51
52 static const WCHAR win32_biosW[] =
53 {'W','i','n','3','2','_','B','I','O','S',0};
54 static const WCHAR win32_computersystemW[] =
55 {'W','i','n','3','2','_','C','o','m','p','u','t','e','r','S','y','s','t','e','m',0};
56 static const WCHAR win32_logicaldiskW[] =
57 {'W','i','n','3','2','_','L','o','g','i','c','a','l','D','i','s','k',0};
58 static const WCHAR win32_networkadapterW[] =
59 {'W','i','n','3','2','_','N','e','t','w','o','r','k','A','d','a','p','t','e','r',0};
60 static const WCHAR win32_operatingsystemW[] =
61 {'W','i','n','3','2','_','O','p','e','r','a','t','i','n','g','S','y','s','t','e','m',0};
62 static const WCHAR win32_processW[] =
63 {'W','i','n','3','2','_','P','r','o','c','e','s','s',0};
64 static const WCHAR win32_processorW[] =
65 {'W','i','n','3','2','_','P','r','o','c','e','s','s','o','r',0};
66
67 static const struct
68 {
69 const WCHAR *alias;
70 const WCHAR *class;
71 }
72 alias_map[] =
73 {
74 { biosW, win32_biosW },
75 { computersystemW, win32_computersystemW },
76 { cpuW, win32_processorW },
77 { logicaldiskW, win32_logicaldiskW },
78 { nicW, win32_networkadapterW },
79 { osW, win32_operatingsystemW },
80 { processW, win32_processW }
81 };
82
83 static const WCHAR *find_class( const WCHAR *alias )
84 {
85 unsigned int i;
86
87 for (i = 0; i < sizeof(alias_map)/sizeof(alias_map[0]); i++)
88 {
89 if (!strcmpiW( alias, alias_map[i].alias )) return alias_map[i].class;
90 }
91 return NULL;
92 }
93
94 static inline WCHAR *strdupW( const WCHAR *src )
95 {
96 WCHAR *dst;
97 if (!src) return NULL;
98 if (!(dst = HeapAlloc( GetProcessHeap(), 0, (strlenW( src ) + 1) * sizeof(WCHAR) ))) return NULL;
99 strcpyW( dst, src );
100 return dst;
101 }
102
103 static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
104 {
105 SAFEARRAY *sa;
106 WCHAR *ret = NULL;
107 LONG i, last_index = 0;
108 BSTR str;
109
110 if (IWbemClassObject_GetNames( class, NULL, WBEM_FLAG_ALWAYS, NULL, &sa ) != S_OK) return NULL;
111
112 SafeArrayGetUBound( sa, 1, &last_index );
113 for (i = 0; i <= last_index; i++)
114 {
115 SafeArrayGetElement( sa, &i, &str );
116 if (!strcmpiW( str, prop ))
117 {
118 ret = strdupW( str );
119 break;
120 }
121 }
122 SafeArrayDestroy( sa );
123 return ret;
124 }
125
126 static int output_string( const WCHAR *msg, ... )
127 {
128 va_list va_args;
129 int wlen;
130 DWORD count, ret;
131 WCHAR buffer[8192];
132
133 va_start( va_args, msg );
134 vsprintfW( buffer, msg, va_args );
135 va_end( va_args );
136
137 wlen = strlenW( buffer );
138 ret = WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, wlen, &count, NULL );
139 if (!ret)
140 {
141 DWORD len;
142 char *msgA;
143
144 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
145 * back to WriteFile(), assuming the console encoding is still the right
146 * one in that case.
147 */
148 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, NULL, 0, NULL, NULL );
149 if (!(msgA = HeapAlloc( GetProcessHeap(), 0, len * sizeof(char) ))) return 0;
150
151 WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, msgA, len, NULL, NULL );
152 WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE );
153 HeapFree( GetProcessHeap(), 0, msgA );
154 }
155 return count;
156 }
157
158 static int output_message( int msg )
159 {
160 static const WCHAR fmtW[] = {'%','s',0};
161 WCHAR buffer[8192];
162
163 LoadStringW( GetModuleHandleW(NULL), msg, buffer, sizeof(buffer)/sizeof(WCHAR) );
164 return output_string( fmtW, buffer );
165 }
166
167 static int query_prop( const WCHAR *alias, const WCHAR *propname )
168 {
169 static const WCHAR select_allW[] = {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',0};
170 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
171 static const WCHAR wqlW[] = {'W','Q','L',0};
172 static const WCHAR newlineW[] = {'\n',0};
173 static const WCHAR fmtW[] = {'%','s','\n',0};
174 HRESULT hr;
175 IWbemLocator *locator = NULL;
176 IWbemServices *services = NULL;
177 IEnumWbemClassObject *result = NULL;
178 LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
179 BSTR path = NULL, wql = NULL, query = NULL;
180 const WCHAR *class;
181 WCHAR *prop = NULL;
182 BOOL first = TRUE;
183 int len, ret = -1;
184
185 WINE_TRACE("%s, %s\n", debugstr_w(alias), debugstr_w(propname));
186
187 if (!(class = find_class( alias )))
188 {
189 output_message( STRING_ALIAS_NOT_FOUND );
190 return -1;
191 }
192 CoInitialize( NULL );
193 CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
194 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
195
196 hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
197 (void **)&locator );
198 if (hr != S_OK) goto done;
199
200 if (!(path = SysAllocString( cimv2W ))) goto done;
201 hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
202 if (hr != S_OK) goto done;
203
204 len = strlenW( class ) + sizeof(select_allW) / sizeof(select_allW[0]);
205 if (!(query = SysAllocStringLen( NULL, len ))) goto done;
206 strcpyW( query, select_allW );
207 strcatW( query, class );
208
209 if (!(wql = SysAllocString( wqlW ))) goto done;
210 hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, &result );
211 if (hr != S_OK) goto done;
212
213 for (;;)
214 {
215 IWbemClassObject *obj;
216 ULONG count;
217 VARIANT v;
218
219 IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
220 if (!count) break;
221
222 if (first)
223 {
224 if (!(prop = find_prop( obj, propname )))
225 {
226 output_message( STRING_INVALID_QUERY );
227 goto done;
228 }
229 output_string( fmtW, prop );
230 first = FALSE;
231 }
232 if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
233 {
234 VariantChangeType( &v, &v, 0, VT_BSTR );
235 output_string( fmtW, V_BSTR( &v ) );
236 VariantClear( &v );
237 }
238 IWbemClassObject_Release( obj );
239 }
240 output_string( newlineW );
241 ret = 0;
242
243 done:
244 if (result) IEnumWbemClassObject_Release( result );
245 if (services) IWbemServices_Release( services );
246 if (locator) IWbemLocator_Release( locator );
247 SysFreeString( path );
248 SysFreeString( query );
249 SysFreeString( wql );
250 HeapFree( GetProcessHeap(), 0, prop );
251 CoUninitialize();
252 return ret;
253 }
254
255 int wmain(int argc, WCHAR *argv[])
256 {
257 static const WCHAR getW[] = {'g','e','t',0};
258
259 if (argc != 4 || strcmpiW( argv[2], getW ))
260 {
261 output_message( STRING_CMDLINE_NOT_SUPPORTED );
262 return -1;
263 }
264 return query_prop( argv[1], argv[3] );
265 }