2010-12-06 23:04:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Louis Lenders
|
2012-06-19 10:19:59 +02:00
|
|
|
* Copyright 2012 Hans Leidekker for CodeWeavers
|
2010-12-06 23:04:20 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-06-19 10:19:59 +02:00
|
|
|
#define COBJMACROS
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "windows.h"
|
|
|
|
#include "ocidl.h"
|
|
|
|
#include "initguid.h"
|
|
|
|
#include "objidl.h"
|
|
|
|
#include "wbemcli.h"
|
|
|
|
#include "wmic.h"
|
|
|
|
|
2010-12-06 23:04:20 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(wmic);
|
|
|
|
|
2012-06-19 10:19:59 +02:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
const WCHAR *alias;
|
|
|
|
const WCHAR *class;
|
|
|
|
}
|
|
|
|
alias_map[] =
|
|
|
|
{
|
2020-10-25 23:40:42 +01:00
|
|
|
{ L"bios", L"Win32_BIOS" },
|
|
|
|
{ L"computersystem", L"Win32_ComputerSystem" },
|
|
|
|
{ L"cpu", L"Win32_Processor" },
|
|
|
|
{ L"LogicalDisk", L"Win32_LogicalDisk" },
|
|
|
|
{ L"nic", L"Win32_NetworkAdapter" },
|
|
|
|
{ L"os", L"Win32_OperatingSystem" },
|
|
|
|
{ L"process", L"Win32_Process" }
|
2012-06-19 10:19:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const WCHAR *find_class( const WCHAR *alias )
|
2010-12-06 23:04:20 +01:00
|
|
|
{
|
2012-06-19 10:19:59 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
2018-07-13 19:01:29 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(alias_map); i++)
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
2019-05-02 09:31:22 +02:00
|
|
|
if (!wcsicmp( alias, alias_map[i].alias )) return alias_map[i].class;
|
2012-06-19 10:19:59 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline WCHAR *strdupW( const WCHAR *src )
|
|
|
|
{
|
|
|
|
WCHAR *dst;
|
|
|
|
if (!src) return NULL;
|
2019-05-02 09:31:22 +02:00
|
|
|
if (!(dst = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( src ) + 1) * sizeof(WCHAR) ))) return NULL;
|
|
|
|
lstrcpyW( dst, src );
|
2012-06-19 10:19:59 +02:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
|
|
|
|
{
|
|
|
|
SAFEARRAY *sa;
|
|
|
|
WCHAR *ret = NULL;
|
|
|
|
LONG i, last_index = 0;
|
|
|
|
BSTR str;
|
|
|
|
|
|
|
|
if (IWbemClassObject_GetNames( class, NULL, WBEM_FLAG_ALWAYS, NULL, &sa ) != S_OK) return NULL;
|
|
|
|
|
|
|
|
SafeArrayGetUBound( sa, 1, &last_index );
|
|
|
|
for (i = 0; i <= last_index; i++)
|
|
|
|
{
|
|
|
|
SafeArrayGetElement( sa, &i, &str );
|
2019-05-02 09:31:22 +02:00
|
|
|
if (!wcsicmp( str, prop ))
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
|
|
|
ret = strdupW( str );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SafeArrayDestroy( sa );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:31:22 +02:00
|
|
|
static int WINAPIV output_string( HANDLE handle, const WCHAR *msg, ... )
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
2019-05-02 09:31:22 +02:00
|
|
|
__ms_va_list va_args;
|
2018-10-08 15:53:10 +02:00
|
|
|
int len;
|
|
|
|
DWORD count;
|
2012-06-19 10:19:59 +02:00
|
|
|
WCHAR buffer[8192];
|
|
|
|
|
2019-05-02 09:31:22 +02:00
|
|
|
__ms_va_start( va_args, msg );
|
|
|
|
len = vswprintf( buffer, ARRAY_SIZE(buffer), msg, va_args );
|
|
|
|
__ms_va_end( va_args );
|
2012-06-19 10:19:59 +02:00
|
|
|
|
2018-10-08 15:53:10 +02:00
|
|
|
if (!WriteConsoleW( handle, buffer, len, &count, NULL ))
|
|
|
|
WriteFile( handle, buffer, len * sizeof(WCHAR), &count, FALSE );
|
|
|
|
|
2012-06-19 10:19:59 +02:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:53:08 +02:00
|
|
|
static int output_error( int msg )
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
|
|
|
WCHAR buffer[8192];
|
|
|
|
|
2018-07-13 19:01:29 +02:00
|
|
|
LoadStringW( GetModuleHandleW(NULL), msg, buffer, ARRAY_SIZE(buffer));
|
2020-10-25 23:40:42 +01:00
|
|
|
return output_string( GetStdHandle(STD_ERROR_HANDLE), L"%s", buffer );
|
2012-06-19 10:19:59 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:53:12 +02:00
|
|
|
static int output_header( const WCHAR *prop, ULONG column_width )
|
2018-10-08 15:53:10 +02:00
|
|
|
{
|
2020-10-25 23:40:42 +01:00
|
|
|
static const WCHAR bomW[] = {0xfeff};
|
2018-10-08 15:53:10 +02:00
|
|
|
int len;
|
|
|
|
DWORD count;
|
|
|
|
WCHAR buffer[8192];
|
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
len = swprintf( buffer, ARRAY_SIZE(buffer), L"%-*s\r\n", column_width, prop );
|
2018-10-08 15:53:10 +02:00
|
|
|
|
|
|
|
if (!WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, &count, NULL )) /* redirected */
|
|
|
|
{
|
|
|
|
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), bomW, sizeof(bomW), &count, FALSE );
|
|
|
|
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), buffer, len * sizeof(WCHAR), &count, FALSE );
|
|
|
|
count += sizeof(bomW);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:53:12 +02:00
|
|
|
static int output_line( const WCHAR *str, ULONG column_width )
|
2018-10-08 15:53:10 +02:00
|
|
|
{
|
2020-10-25 23:40:42 +01:00
|
|
|
return output_string( GetStdHandle(STD_OUTPUT_HANDLE), L"%-*s\r\n", column_width, str );
|
2018-10-08 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 05:58:17 +02:00
|
|
|
static int query_prop( const WCHAR *class, const WCHAR *propname )
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
IWbemLocator *locator = NULL;
|
|
|
|
IWbemServices *services = NULL;
|
|
|
|
IEnumWbemClassObject *result = NULL;
|
|
|
|
LONG flags = WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY;
|
|
|
|
BSTR path = NULL, wql = NULL, query = NULL;
|
|
|
|
WCHAR *prop = NULL;
|
|
|
|
BOOL first = TRUE;
|
|
|
|
int len, ret = -1;
|
2018-10-08 15:53:12 +02:00
|
|
|
IWbemClassObject *obj;
|
|
|
|
ULONG count, width = 0;
|
|
|
|
VARIANT v;
|
2012-06-19 10:19:59 +02:00
|
|
|
|
2016-10-14 05:58:17 +02:00
|
|
|
WINE_TRACE("%s, %s\n", debugstr_w(class), debugstr_w(propname));
|
2012-06-19 10:19:59 +02:00
|
|
|
|
|
|
|
CoInitialize( NULL );
|
|
|
|
CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
|
|
|
|
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
|
|
|
|
|
|
|
|
hr = CoCreateInstance( &CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemLocator,
|
|
|
|
(void **)&locator );
|
|
|
|
if (hr != S_OK) goto done;
|
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
if (!(path = SysAllocString(L"ROOT\\CIMV2" ))) goto done;
|
2012-06-19 10:19:59 +02:00
|
|
|
hr = IWbemLocator_ConnectServer( locator, path, NULL, NULL, NULL, 0, NULL, NULL, &services );
|
|
|
|
if (hr != S_OK) goto done;
|
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
len = lstrlenW( class ) + ARRAY_SIZE(L"SELECT * FROM ");
|
2012-06-19 10:19:59 +02:00
|
|
|
if (!(query = SysAllocStringLen( NULL, len ))) goto done;
|
2020-10-25 23:40:42 +01:00
|
|
|
lstrcpyW( query, L"SELECT * FROM " );
|
2019-05-02 09:31:22 +02:00
|
|
|
lstrcatW( query, class );
|
2012-06-19 10:19:59 +02:00
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
if (!(wql = SysAllocString(L"WQL" ))) goto done;
|
2012-06-19 10:19:59 +02:00
|
|
|
hr = IWbemServices_ExecQuery( services, wql, query, flags, NULL, &result );
|
|
|
|
if (hr != S_OK) goto done;
|
|
|
|
|
2018-10-08 15:53:12 +02:00
|
|
|
for (;;) /* get column width */
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
2018-10-08 15:53:12 +02:00
|
|
|
IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
|
|
|
|
if (!count) break;
|
2012-06-19 10:19:59 +02:00
|
|
|
|
2018-10-08 15:53:12 +02:00
|
|
|
if (!prop && !(prop = find_prop( obj, propname )))
|
|
|
|
{
|
|
|
|
output_error( STRING_INVALID_QUERY );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
|
|
|
|
{
|
|
|
|
VariantChangeType( &v, &v, 0, VT_BSTR );
|
2019-05-02 09:31:22 +02:00
|
|
|
width = max( lstrlenW( V_BSTR( &v ) ), width );
|
2018-10-08 15:53:12 +02:00
|
|
|
VariantClear( &v );
|
|
|
|
}
|
|
|
|
IWbemClassObject_Release( obj );
|
|
|
|
}
|
|
|
|
width += 2;
|
|
|
|
|
|
|
|
IEnumWbemClassObject_Reset( result );
|
|
|
|
for (;;)
|
|
|
|
{
|
2012-06-19 10:19:59 +02:00
|
|
|
IEnumWbemClassObject_Next( result, WBEM_INFINITE, 1, &obj, &count );
|
|
|
|
if (!count) break;
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
{
|
2018-10-08 15:53:12 +02:00
|
|
|
output_header( prop, width );
|
2012-06-19 10:19:59 +02:00
|
|
|
first = FALSE;
|
|
|
|
}
|
|
|
|
if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
|
|
|
|
{
|
2013-10-01 11:35:46 +02:00
|
|
|
VariantChangeType( &v, &v, 0, VT_BSTR );
|
2018-10-08 15:53:12 +02:00
|
|
|
output_line( V_BSTR( &v ), width );
|
2012-06-19 10:19:59 +02:00
|
|
|
VariantClear( &v );
|
|
|
|
}
|
|
|
|
IWbemClassObject_Release( obj );
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (result) IEnumWbemClassObject_Release( result );
|
|
|
|
if (services) IWbemServices_Release( services );
|
|
|
|
if (locator) IWbemLocator_Release( locator );
|
|
|
|
SysFreeString( path );
|
|
|
|
SysFreeString( query );
|
|
|
|
SysFreeString( wql );
|
|
|
|
HeapFree( GetProcessHeap(), 0, prop );
|
|
|
|
CoUninitialize();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain(int argc, WCHAR *argv[])
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
2016-10-14 05:58:17 +02:00
|
|
|
const WCHAR *class, *value;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < argc && argv[i][0] == '/'; i++)
|
|
|
|
WINE_FIXME( "command line switch %s not supported\n", debugstr_w(argv[i]) );
|
|
|
|
|
|
|
|
if (i >= argc)
|
|
|
|
goto not_supported;
|
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
if (!wcsicmp( argv[i], L"quit" ) || !wcsicmp( argv[i], L"exit" ))
|
2016-10-14 05:58:17 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-06 23:04:20 +01:00
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
if (!wcsicmp( argv[i], L"class") || !wcsicmp( argv[i], L"context" ))
|
2012-06-19 10:19:59 +02:00
|
|
|
{
|
2016-10-14 05:58:17 +02:00
|
|
|
WINE_FIXME( "command %s not supported\n", debugstr_w(argv[i]) );
|
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
if (!wcsicmp( argv[i], L"path" ))
|
2016-10-14 05:58:17 +02:00
|
|
|
{
|
|
|
|
if (++i >= argc)
|
|
|
|
{
|
2018-10-08 15:53:08 +02:00
|
|
|
output_error( STRING_INVALID_PATH );
|
2016-10-14 05:58:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
class = argv[i];
|
2012-06-19 10:19:59 +02:00
|
|
|
}
|
2016-10-14 05:58:17 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
class = find_class( argv[i] );
|
|
|
|
if (!class)
|
|
|
|
{
|
2018-10-08 15:53:08 +02:00
|
|
|
output_error( STRING_ALIAS_NOT_FOUND );
|
2016-10-14 05:58:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++i >= argc)
|
|
|
|
goto not_supported;
|
|
|
|
|
2020-10-25 23:40:42 +01:00
|
|
|
if (!wcsicmp( argv[i], L"get" ))
|
2016-10-14 05:58:17 +02:00
|
|
|
{
|
|
|
|
if (++i >= argc)
|
|
|
|
goto not_supported;
|
|
|
|
value = argv[i];
|
|
|
|
return query_prop( class, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
not_supported:
|
2018-10-08 15:53:08 +02:00
|
|
|
output_error( STRING_CMDLINE_NOT_SUPPORTED );
|
2016-10-14 05:58:17 +02:00
|
|
|
return 1;
|
2010-12-06 23:04:20 +01:00
|
|
|
}
|