main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 struct
  30. {
  31. const WCHAR *alias;
  32. const WCHAR *class;
  33. }
  34. alias_map[] =
  35. {
  36. { L"bios", L"Win32_BIOS" },
  37. { L"computersystem", L"Win32_ComputerSystem" },
  38. { L"cpu", L"Win32_Processor" },
  39. { L"LogicalDisk", L"Win32_LogicalDisk" },
  40. { L"nic", L"Win32_NetworkAdapter" },
  41. { L"os", L"Win32_OperatingSystem" },
  42. { L"process", L"Win32_Process" },
  43. { L"baseboard", L"Win32_BaseBoard" },
  44. { L"diskdrive", L"Win32_DiskDrive" },
  45. { L"memorychip", L"Win32_PhysicalMemory" }
  46. };
  47. static const WCHAR *find_class( const WCHAR *alias )
  48. {
  49. unsigned int i;
  50. for (i = 0; i < ARRAY_SIZE(alias_map); i++)
  51. {
  52. if (!wcsicmp( alias, alias_map[i].alias )) return alias_map[i].class;
  53. }
  54. return NULL;
  55. }
  56. static inline WCHAR *strdupW( const WCHAR *src )
  57. {
  58. WCHAR *dst;
  59. if (!src) return NULL;
  60. if (!(dst = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( src ) + 1) * sizeof(WCHAR) ))) return NULL;
  61. lstrcpyW( dst, src );
  62. return dst;
  63. }
  64. static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
  65. {
  66. SAFEARRAY *sa;
  67. WCHAR *ret = NULL;
  68. LONG i, last_index = 0;
  69. BSTR str;
  70. if (IWbemClassObject_GetNames( class, NULL, WBEM_FLAG_ALWAYS, NULL, &sa ) != S_OK) return NULL;
  71. SafeArrayGetUBound( sa, 1, &last_index );
  72. for (i = 0; i <= last_index; i++)
  73. {
  74. SafeArrayGetElement( sa, &i, &str );
  75. if (!wcsicmp( str, prop ))
  76. {
  77. ret = strdupW( str );
  78. break;
  79. }
  80. }
  81. SafeArrayDestroy( sa );
  82. return ret;
  83. }
  84. static int WINAPIV output_string( HANDLE handle, const WCHAR *msg, ... )
  85. {
  86. __ms_va_list va_args;
  87. int len;
  88. DWORD count;
  89. WCHAR buffer[8192];
  90. __ms_va_start( va_args, msg );
  91. len = vswprintf( buffer, ARRAY_SIZE(buffer), msg, va_args );
  92. __ms_va_end( va_args );
  93. if (!WriteConsoleW( handle, buffer, len, &count, NULL ))
  94. WriteFile( handle, buffer, len * sizeof(WCHAR), &count, FALSE );
  95. return count;
  96. }
  97. static int output_error( int msg )
  98. {
  99. WCHAR buffer[8192];
  100. LoadStringW( GetModuleHandleW(NULL), msg, buffer, ARRAY_SIZE(buffer));
  101. return output_string( GetStdHandle(STD_ERROR_HANDLE), L"%s", buffer );
  102. }
  103. static int output_header( const WCHAR *prop, ULONG column_width )
  104. {
  105. static const WCHAR bomW[] = {0xfeff};
  106. int len;
  107. DWORD count;
  108. WCHAR buffer[8192];
  109. len = swprintf( buffer, ARRAY_SIZE(buffer), L"%-*s\r\n", column_width, prop );
  110. if (!WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, &count, NULL )) /* redirected */
  111. {
  112. WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), bomW, sizeof(bomW), &count, FALSE );
  113. WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len * sizeof(WCHAR), &count, FALSE );
  114. count += sizeof(bomW);
  115. }
  116. return count;
  117. }
  118. static int output_line( const WCHAR *str, ULONG column_width )
  119. {
  120. return output_string( GetStdHandle(STD_OUTPUT_HANDLE), L"%-*s\r\n", column_width, str );
  121. }
  122. static int query_prop( const WCHAR *class, const WCHAR *propname )
  123. {
  124. HRESULT hr;
  125. IWbemLocator *locator = NULL;
  126. IWbemServices *services = NULL;
  127. IEnumWbemClassObject *result = NULL;
  128. LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
  129. BSTR path = NULL, wql = NULL, query = NULL;
  130. WCHAR *prop = NULL;
  131. BOOL first = TRUE;
  132. int len, ret = -1;
  133. IWbemClassObject *obj;
  134. ULONG count, width = 0;
  135. VARIANT v;
  136. WINE_TRACE("%s, %s\n", debugstr_w(class), debugstr_w(propname));
  137. CoInitialize( NULL );
  138. CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
  139. RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
  140. hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
  141. (void **)&locator );
  142. if (hr != S_OK) goto done;
  143. if (!(path = SysAllocString(L"ROOT\\CIMV2" ))) goto done;
  144. hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
  145. if (hr != S_OK) goto done;
  146. len = lstrlenW( class ) + ARRAY_SIZE(L"SELECT * FROM ");
  147. if (!(query = SysAllocStringLen( NULL, len ))) goto done;
  148. lstrcpyW( query, L"SELECT * FROM " );
  149. lstrcatW( query, class );
  150. if (!(wql = SysAllocString(L"WQL" ))) goto done;
  151. hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, &result );
  152. if (hr != S_OK) goto done;
  153. for (;;) /* get column width */
  154. {
  155. IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
  156. if (!count) break;
  157. if (!prop && !(prop = find_prop( obj, propname )))
  158. {
  159. output_error( STRING_INVALID_QUERY );
  160. goto done;
  161. }
  162. if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
  163. {
  164. VariantChangeType( &v, &v, 0, VT_BSTR );
  165. width = max( lstrlenW( V_BSTR( &v ) ), width );
  166. VariantClear( &v );
  167. }
  168. IWbemClassObject_Release( obj );
  169. }
  170. width += 2;
  171. IEnumWbemClassObject_Reset( result );
  172. for (;;)
  173. {
  174. IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
  175. if (!count) break;
  176. if (first)
  177. {
  178. output_header( prop, width );
  179. first = FALSE;
  180. }
  181. if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
  182. {
  183. VariantChangeType( &v, &v, 0, VT_BSTR );
  184. output_line( V_BSTR( &v ), width );
  185. VariantClear( &v );
  186. }
  187. IWbemClassObject_Release( obj );
  188. }
  189. ret = 0;
  190. done:
  191. if (result) IEnumWbemClassObject_Release( result );
  192. if (services) IWbemServices_Release( services );
  193. if (locator) IWbemLocator_Release( locator );
  194. SysFreeString( path );
  195. SysFreeString( query );
  196. SysFreeString( wql );
  197. HeapFree( GetProcessHeap(), 0, prop );
  198. CoUninitialize();
  199. return ret;
  200. }
  201. int __cdecl wmain(int argc, WCHAR *argv[])
  202. {
  203. const WCHAR *class, *value;
  204. int i;
  205. for (i = 1; i < argc && argv[i][0] == '/'; i++)
  206. WINE_FIXME( "command line switch %s not supported\n", debugstr_w(argv[i]) );
  207. if (i >= argc)
  208. goto not_supported;
  209. if (!wcsicmp( argv[i], L"quit" ) || !wcsicmp( argv[i], L"exit" ))
  210. {
  211. return 0;
  212. }
  213. if (!wcsicmp( argv[i], L"class") || !wcsicmp( argv[i], L"context" ))
  214. {
  215. WINE_FIXME( "command %s not supported\n", debugstr_w(argv[i]) );
  216. goto not_supported;
  217. }
  218. if (!wcsicmp( argv[i], L"path" ))
  219. {
  220. if (++i >= argc)
  221. {
  222. output_error( STRING_INVALID_PATH );
  223. return 1;
  224. }
  225. class = argv[i];
  226. }
  227. else
  228. {
  229. class = find_class( argv[i] );
  230. if (!class)
  231. {
  232. output_error( STRING_ALIAS_NOT_FOUND );
  233. return 1;
  234. }
  235. }
  236. if (++i >= argc)
  237. goto not_supported;
  238. if (!wcsicmp( argv[i], L"get" ))
  239. {
  240. if (++i >= argc)
  241. goto not_supported;
  242. value = argv[i];
  243. return query_prop( class, value );
  244. }
  245. not_supported:
  246. output_error( STRING_CMDLINE_NOT_SUPPORTED );
  247. return 1;
  248. }