main.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #define COBJMACROS
  20. #include <stdio.h>
  21. #include "windows.h"
  22. #include "ocidl.h"
  23. #include "initguid.h"
  24. #include "objidl.h"
  25. #include "wbemcli.h"
  26. #include "wmic.h"
  27. #include "wine/debug.h"
  28. WINE_DEFAULT_DEBUG_CHANNEL(wmic);
  29. static const WCHAR biosW[] =
  30. {'b','i','o','s',0};
  31. static const WCHAR computersystemW[] =
  32. {'c','o','m','p','u','t','e','r','s','y','s','t','e','m',0};
  33. static const WCHAR cpuW[] =
  34. {'c','p','u',0};
  35. static const WCHAR logicaldiskW[] =
  36. {'L','o','g','i','c','a','l','D','i','s','k',0};
  37. static const WCHAR nicW[] =
  38. {'n','i','c',0};
  39. static const WCHAR osW[] =
  40. {'o','s',0};
  41. static const WCHAR processW[] =
  42. {'p','r','o','c','e','s','s',0};
  43. static const WCHAR win32_biosW[] =
  44. {'W','i','n','3','2','_','B','I','O','S',0};
  45. static const WCHAR win32_computersystemW[] =
  46. {'W','i','n','3','2','_','C','o','m','p','u','t','e','r','S','y','s','t','e','m',0};
  47. static const WCHAR win32_logicaldiskW[] =
  48. {'W','i','n','3','2','_','L','o','g','i','c','a','l','D','i','s','k',0};
  49. static const WCHAR win32_networkadapterW[] =
  50. {'W','i','n','3','2','_','N','e','t','w','o','r','k','A','d','a','p','t','e','r',0};
  51. static const WCHAR win32_operatingsystemW[] =
  52. {'W','i','n','3','2','_','O','p','e','r','a','t','i','n','g','S','y','s','t','e','m',0};
  53. static const WCHAR win32_processW[] =
  54. {'W','i','n','3','2','_','P','r','o','c','e','s','s',0};
  55. static const WCHAR win32_processorW[] =
  56. {'W','i','n','3','2','_','P','r','o','c','e','s','s','o','r',0};
  57. static const struct
  58. {
  59. const WCHAR *alias;
  60. const WCHAR *class;
  61. }
  62. alias_map[] =
  63. {
  64. { biosW, win32_biosW },
  65. { computersystemW, win32_computersystemW },
  66. { cpuW, win32_processorW },
  67. { logicaldiskW, win32_logicaldiskW },
  68. { nicW, win32_networkadapterW },
  69. { osW, win32_operatingsystemW },
  70. { processW, win32_processW }
  71. };
  72. static const WCHAR *find_class( const WCHAR *alias )
  73. {
  74. unsigned int i;
  75. for (i = 0; i < ARRAY_SIZE(alias_map); i++)
  76. {
  77. if (!wcsicmp( alias, alias_map[i].alias )) return alias_map[i].class;
  78. }
  79. return NULL;
  80. }
  81. static inline WCHAR *strdupW( const WCHAR *src )
  82. {
  83. WCHAR *dst;
  84. if (!src) return NULL;
  85. if (!(dst = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( src ) + 1) * sizeof(WCHAR) ))) return NULL;
  86. lstrcpyW( dst, src );
  87. return dst;
  88. }
  89. static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
  90. {
  91. SAFEARRAY *sa;
  92. WCHAR *ret = NULL;
  93. LONG i, last_index = 0;
  94. BSTR str;
  95. if (IWbemClassObject_GetNames( class, NULL, WBEM_FLAG_ALWAYS, NULL, &sa ) != S_OK) return NULL;
  96. SafeArrayGetUBound( sa, 1, &last_index );
  97. for (i = 0; i <= last_index; i++)
  98. {
  99. SafeArrayGetElement( sa, &i, &str );
  100. if (!wcsicmp( str, prop ))
  101. {
  102. ret = strdupW( str );
  103. break;
  104. }
  105. }
  106. SafeArrayDestroy( sa );
  107. return ret;
  108. }
  109. static int WINAPIV output_string( HANDLE handle, const WCHAR *msg, ... )
  110. {
  111. __ms_va_list va_args;
  112. int len;
  113. DWORD count;
  114. WCHAR buffer[8192];
  115. __ms_va_start( va_args, msg );
  116. len = vswprintf( buffer, ARRAY_SIZE(buffer), msg, va_args );
  117. __ms_va_end( va_args );
  118. if (!WriteConsoleW( handle, buffer, len, &count, NULL ))
  119. WriteFile( handle, buffer, len * sizeof(WCHAR), &count, FALSE );
  120. return count;
  121. }
  122. static int output_error( int msg )
  123. {
  124. static const WCHAR fmtW[] = {'%','s',0};
  125. WCHAR buffer[8192];
  126. LoadStringW( GetModuleHandleW(NULL), msg, buffer, ARRAY_SIZE(buffer));
  127. return output_string( GetStdHandle(STD_ERROR_HANDLE), fmtW, buffer );
  128. }
  129. static int output_header( const WCHAR *prop, ULONG column_width )
  130. {
  131. static const WCHAR bomW[] = {0xfeff}, fmtW[] = {'%','-','*','s','\r','\n',0};
  132. int len;
  133. DWORD count;
  134. WCHAR buffer[8192];
  135. len = swprintf( buffer, ARRAY_SIZE(buffer), fmtW, column_width, prop );
  136. if (!WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, &count, NULL )) /* redirected */
  137. {
  138. WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), bomW, sizeof(bomW), &count, FALSE );
  139. WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len * sizeof(WCHAR), &count, FALSE );
  140. count += sizeof(bomW);
  141. }
  142. return count;
  143. }
  144. static int output_line( const WCHAR *str, ULONG column_width )
  145. {
  146. static const WCHAR fmtW[] = {'%','-','*','s','\r','\n',0};
  147. return output_string( GetStdHandle(STD_OUTPUT_HANDLE), fmtW, column_width, str );
  148. }
  149. static int query_prop( const WCHAR *class, const WCHAR *propname )
  150. {
  151. static const WCHAR select_allW[] = {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',0};
  152. static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
  153. static const WCHAR wqlW[] = {'W','Q','L',0};
  154. HRESULT hr;
  155. IWbemLocator *locator = NULL;
  156. IWbemServices *services = NULL;
  157. IEnumWbemClassObject *result = NULL;
  158. LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
  159. BSTR path = NULL, wql = NULL, query = NULL;
  160. WCHAR *prop = NULL;
  161. BOOL first = TRUE;
  162. int len, ret = -1;
  163. IWbemClassObject *obj;
  164. ULONG count, width = 0;
  165. VARIANT v;
  166. WINE_TRACE("%s, %s\n", debugstr_w(class), debugstr_w(propname));
  167. CoInitialize( NULL );
  168. CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
  169. RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
  170. hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
  171. (void **)&locator );
  172. if (hr != S_OK) goto done;
  173. if (!(path = SysAllocString( cimv2W ))) goto done;
  174. hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
  175. if (hr != S_OK) goto done;
  176. len = lstrlenW( class ) + ARRAY_SIZE(select_allW);
  177. if (!(query = SysAllocStringLen( NULL, len ))) goto done;
  178. lstrcpyW( query, select_allW );
  179. lstrcatW( query, class );
  180. if (!(wql = SysAllocString( wqlW ))) goto done;
  181. hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, &result );
  182. if (hr != S_OK) goto done;
  183. for (;;) /* get column width */
  184. {
  185. IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
  186. if (!count) break;
  187. if (!prop && !(prop = find_prop( obj, propname )))
  188. {
  189. output_error( STRING_INVALID_QUERY );
  190. goto done;
  191. }
  192. if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
  193. {
  194. VariantChangeType( &v, &v, 0, VT_BSTR );
  195. width = max( lstrlenW( V_BSTR( &v ) ), width );
  196. VariantClear( &v );
  197. }
  198. IWbemClassObject_Release( obj );
  199. }
  200. width += 2;
  201. IEnumWbemClassObject_Reset( result );
  202. for (;;)
  203. {
  204. IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
  205. if (!count) break;
  206. if (first)
  207. {
  208. output_header( prop, width );
  209. first = FALSE;
  210. }
  211. if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
  212. {
  213. VariantChangeType( &v, &v, 0, VT_BSTR );
  214. output_line( V_BSTR( &v ), width );
  215. VariantClear( &v );
  216. }
  217. IWbemClassObject_Release( obj );
  218. }
  219. ret = 0;
  220. done:
  221. if (result) IEnumWbemClassObject_Release( result );
  222. if (services) IWbemServices_Release( services );
  223. if (locator) IWbemLocator_Release( locator );
  224. SysFreeString( path );
  225. SysFreeString( query );
  226. SysFreeString( wql );
  227. HeapFree( GetProcessHeap(), 0, prop );
  228. CoUninitialize();
  229. return ret;
  230. }
  231. int __cdecl wmain(int argc, WCHAR *argv[])
  232. {
  233. static const WCHAR getW[] = {'g','e','t',0};
  234. static const WCHAR quitW[] = {'q','u','i','t',0};
  235. static const WCHAR exitW[] = {'e','x','i','t',0};
  236. static const WCHAR pathW[] = {'p','a','t','h',0};
  237. static const WCHAR classW[] = {'c','l','a','s','s',0};
  238. static const WCHAR contextW[] = {'c','o','n','t','e','x','t',0};
  239. const WCHAR *class, *value;
  240. int i;
  241. for (i = 1; i < argc && argv[i][0] == '/'; i++)
  242. WINE_FIXME( "command line switch %s not supported\n", debugstr_w(argv[i]) );
  243. if (i >= argc)
  244. goto not_supported;
  245. if (!wcsicmp( argv[i], quitW ) ||
  246. !wcsicmp( argv[i], exitW ))
  247. {
  248. return 0;
  249. }
  250. if (!wcsicmp( argv[i], classW) ||
  251. !wcsicmp( argv[i], contextW ))
  252. {
  253. WINE_FIXME( "command %s not supported\n", debugstr_w(argv[i]) );
  254. goto not_supported;
  255. }
  256. if (!wcsicmp( argv[i], pathW ))
  257. {
  258. if (++i >= argc)
  259. {
  260. output_error( STRING_INVALID_PATH );
  261. return 1;
  262. }
  263. class = argv[i];
  264. }
  265. else
  266. {
  267. class = find_class( argv[i] );
  268. if (!class)
  269. {
  270. output_error( STRING_ALIAS_NOT_FOUND );
  271. return 1;
  272. }
  273. }
  274. if (++i >= argc)
  275. goto not_supported;
  276. if (!wcsicmp( argv[i], getW ))
  277. {
  278. if (++i >= argc)
  279. goto not_supported;
  280. value = argv[i];
  281. return query_prop( class, value );
  282. }
  283. not_supported:
  284. output_error( STRING_CMDLINE_NOT_SUPPORTED );
  285. return 1;
  286. }