Release 1.6-rc2.
[wine/testsucceed.git] / programs / wmic / main.c
blob8d89bf4bcf741c5db7dcbb71964c5352ee2cf11b
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"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wmic);
35 static const WCHAR biosW[] =
36 {'b','i','o','s',0};
37 static const WCHAR computersystemW[] =
38 {'c','o','m','p','u','t','e','r','s','y','s','t','e','m',0};
39 static const WCHAR cpuW[] =
40 {'c','p','u',0};
41 static const WCHAR logicaldiskW[] =
42 {'L','o','g','i','c','a','l','D','i','s','k',0};
43 static const WCHAR nicW[] =
44 {'n','i','c',0};
45 static const WCHAR osW[] =
46 {'o','s',0};
47 static const WCHAR processW[] =
48 {'p','r','o','c','e','s','s',0};
50 static const WCHAR win32_biosW[] =
51 {'W','i','n','3','2','_','B','I','O','S',0};
52 static const WCHAR win32_computersystemW[] =
53 {'W','i','n','3','2','_','C','o','m','p','u','t','e','r','S','y','s','t','e','m',0};
54 static const WCHAR win32_logicaldiskW[] =
55 {'W','i','n','3','2','_','L','o','g','i','c','a','l','D','i','s','k',0};
56 static const WCHAR win32_networkadapterW[] =
57 {'W','i','n','3','2','_','N','e','t','w','o','r','k','A','d','a','p','t','e','r',0};
58 static const WCHAR win32_operatingsystemW[] =
59 {'W','i','n','3','2','_','O','p','e','r','a','t','i','n','g','S','y','s','t','e','m',0};
60 static const WCHAR win32_processW[] =
61 {'W','i','n','3','2','_','P','r','o','c','e','s','s',0};
62 static const WCHAR win32_processorW[] =
63 {'W','i','n','3','2','_','P','r','o','c','e','s','s','o','r',0};
65 static const struct
67 const WCHAR *alias;
68 const WCHAR *class;
70 alias_map[] =
72 { biosW, win32_biosW },
73 { computersystemW, win32_computersystemW },
74 { cpuW, win32_processorW },
75 { logicaldiskW, win32_logicaldiskW },
76 { nicW, win32_networkadapterW },
77 { osW, win32_operatingsystemW },
78 { processW, win32_processW }
81 static const WCHAR *find_class( const WCHAR *alias )
83 unsigned int i;
85 for (i = 0; i < sizeof(alias_map)/sizeof(alias_map[0]); i++)
87 if (!strcmpiW( alias, alias_map[i].alias )) return alias_map[i].class;
89 return NULL;
92 static inline WCHAR *strdupW( const WCHAR *src )
94 WCHAR *dst;
95 if (!src) return NULL;
96 if (!(dst = HeapAlloc( GetProcessHeap(), 0, (strlenW( src ) + 1) * sizeof(WCHAR) ))) return NULL;
97 strcpyW( dst, src );
98 return dst;
101 static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
103 SAFEARRAY *sa;
104 WCHAR *ret = NULL;
105 LONG i, last_index = 0;
106 BSTR str;
108 if (IWbemClassObject_GetNames( class, NULL, WBEM_FLAG_ALWAYS, NULL, &sa ) != S_OK) return NULL;
110 SafeArrayGetUBound( sa, 1, &last_index );
111 for (i = 0; i <= last_index; i++)
113 SafeArrayGetElement( sa, &i, &str );
114 if (!strcmpiW( str, prop ))
116 ret = strdupW( str );
117 break;
120 SafeArrayDestroy( sa );
121 return ret;
124 static int output_string( const WCHAR *msg, ... )
126 va_list va_args;
127 int wlen;
128 DWORD count, ret;
129 WCHAR buffer[8192];
131 va_start( va_args, msg );
132 vsprintfW( buffer, msg, va_args );
133 va_end( va_args );
135 wlen = strlenW( buffer );
136 ret = WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, wlen, &count, NULL );
137 if (!ret)
139 DWORD len;
140 char *msgA;
142 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
143 * back to WriteFile(), assuming the console encoding is still the right
144 * one in that case.
146 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, NULL, 0, NULL, NULL );
147 if (!(msgA = HeapAlloc( GetProcessHeap(), 0, len * sizeof(char) ))) return 0;
149 WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, msgA, len, NULL, NULL );
150 WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE );
151 HeapFree( GetProcessHeap(), 0, msgA );
153 return count;
156 static int output_message( int msg )
158 static const WCHAR fmtW[] = {'%','s',0};
159 WCHAR buffer[8192];
161 LoadStringW( GetModuleHandleW(NULL), msg, buffer, sizeof(buffer)/sizeof(WCHAR) );
162 return output_string( fmtW, buffer );
165 static int query_prop( const WCHAR *alias, const WCHAR *propname )
167 static const WCHAR select_allW[] = {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',0};
168 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
169 static const WCHAR wqlW[] = {'W','Q','L',0};
170 static const WCHAR newlineW[] = {'\n',0};
171 static const WCHAR fmtW[] = {'%','s','\n',0};
172 HRESULT hr;
173 IWbemLocator *locator = NULL;
174 IWbemServices *services = NULL;
175 IEnumWbemClassObject *result = NULL;
176 LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
177 BSTR path = NULL, wql = NULL, query = NULL;
178 const WCHAR *class;
179 WCHAR *prop = NULL;
180 BOOL first = TRUE;
181 int len, ret = -1;
183 WINE_TRACE("%s, %s\n", debugstr_w(alias), debugstr_w(propname));
185 if (!(class = find_class( alias )))
187 output_message( STRING_ALIAS_NOT_FOUND );
188 return -1;
190 CoInitialize( NULL );
191 CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
192 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
194 hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
195 (void **)&locator );
196 if (hr != S_OK) goto done;
198 if (!(path = SysAllocString( cimv2W ))) goto done;
199 hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
200 if (hr != S_OK) goto done;
202 len = strlenW( class ) + sizeof(select_allW) / sizeof(select_allW[0]);
203 if (!(query = SysAllocStringLen( NULL, len ))) goto done;
204 strcpyW( query, select_allW );
205 strcatW( query, class );
207 if (!(wql = SysAllocString( wqlW ))) goto done;
208 hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, &result );
209 if (hr != S_OK) goto done;
211 for (;;)
213 IWbemClassObject *obj;
214 ULONG count;
215 VARIANT v;
217 IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
218 if (!count) break;
220 if (first)
222 if (!(prop = find_prop( obj, propname )))
224 output_message( STRING_INVALID_QUERY );
225 goto done;
227 output_string( fmtW, prop );
228 first = FALSE;
230 if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
232 output_string( fmtW, V_BSTR( &v ) );
233 VariantClear( &v );
235 IWbemClassObject_Release( obj );
237 output_string( newlineW );
238 ret = 0;
240 done:
241 if (result) IEnumWbemClassObject_Release( result );
242 if (services) IWbemServices_Release( services );
243 if (locator) IWbemLocator_Release( locator );
244 SysFreeString( path );
245 SysFreeString( query );
246 SysFreeString( wql );
247 HeapFree( GetProcessHeap(), 0, prop );
248 CoUninitialize();
249 return ret;
252 int wmain(int argc, WCHAR *argv[])
254 static const WCHAR getW[] = {'g','e','t',0};
256 if (argc != 4 || strcmpiW( argv[2], getW ))
258 output_message( STRING_CMDLINE_NOT_SUPPORTED );
259 return -1;
261 return query_prop( argv[1], argv[3] );