wmic: Output Unicode text with BOM when redirected.

Signed-off-by: Hans Leidekker <hans@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Hans Leidekker 2018-10-08 15:53:10 +02:00 committed by Alexandre Julliard
parent 2ccb82b099
commit a7f0dc07b2
1 changed files with 32 additions and 23 deletions

View File

@ -124,32 +124,17 @@ static WCHAR *find_prop( IWbemClassObject *class, const WCHAR *prop )
static int output_string( HANDLE handle, const WCHAR *msg, ... ) static int output_string( HANDLE handle, const WCHAR *msg, ... )
{ {
va_list va_args; va_list va_args;
int wlen; int len;
DWORD count, ret; DWORD count;
WCHAR buffer[8192]; WCHAR buffer[8192];
va_start( va_args, msg ); va_start( va_args, msg );
vsprintfW( buffer, msg, va_args ); len = vsnprintfW( buffer, ARRAY_SIZE(buffer), msg, va_args );
va_end( va_args ); va_end( va_args );
wlen = strlenW( buffer ); if (!WriteConsoleW( handle, buffer, len, &count, NULL ))
ret = WriteConsoleW( handle, buffer, wlen, &count, NULL ); WriteFile( handle, buffer, len * sizeof(WCHAR), &count, FALSE );
if (!ret)
{
DWORD len;
char *msgA;
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
* back to WriteFile(), assuming the console encoding is still the right
* one in that case.
*/
len = WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, NULL, 0, NULL, NULL );
if (!(msgA = HeapAlloc( GetProcessHeap(), 0, len * sizeof(char) ))) return 0;
WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, msgA, len, NULL, NULL );
WriteFile( handle, msgA, len, &count, FALSE );
HeapFree( GetProcessHeap(), 0, msgA );
}
return count; return count;
} }
@ -162,13 +147,37 @@ static int output_error( int msg )
return output_string( GetStdHandle(STD_ERROR_HANDLE), fmtW, buffer ); return output_string( GetStdHandle(STD_ERROR_HANDLE), fmtW, buffer );
} }
static int output_header( const WCHAR *prop )
{
static const WCHAR bomW[] = {0xfeff}, fmtW[] = {'%','s','\r','\n',0};
int len;
DWORD count;
WCHAR buffer[8192];
len = snprintfW( buffer, ARRAY_SIZE(buffer), fmtW, prop );
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;
}
static int output_line( const WCHAR *str )
{
static const WCHAR fmtW[] = {'%','s','\r','\n',0};
return output_string( GetStdHandle(STD_OUTPUT_HANDLE), fmtW, str );
}
static int query_prop( const WCHAR *class, const WCHAR *propname ) static int query_prop( const WCHAR *class, const WCHAR *propname )
{ {
static const WCHAR select_allW[] = {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',0}; static const WCHAR select_allW[] = {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',0};
static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0}; static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
static const WCHAR wqlW[] = {'W','Q','L',0}; static const WCHAR wqlW[] = {'W','Q','L',0};
static const WCHAR newlineW[] = {'\r','\n',0}; static const WCHAR newlineW[] = {'\r','\n',0};
static const WCHAR fmtW[] = {'%','s','\r','\n',0};
HRESULT hr; HRESULT hr;
IWbemLocator *locator = NULL; IWbemLocator *locator = NULL;
IWbemServices *services = NULL; IWbemServices *services = NULL;
@ -218,13 +227,13 @@ static int query_prop( const WCHAR *class, const WCHAR *propname )
output_error( STRING_INVALID_QUERY ); output_error( STRING_INVALID_QUERY );
goto done; goto done;
} }
output_string( GetStdHandle(STD_OUTPUT_HANDLE), fmtW, prop ); output_header( prop );
first = FALSE; first = FALSE;
} }
if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR) if (IWbemClassObject_Get( obj, prop, 0, &v, NULL, NULL ) == WBEM_S_NO_ERROR)
{ {
VariantChangeType( &v, &v, 0, VT_BSTR ); VariantChangeType( &v, &v, 0, VT_BSTR );
output_string( GetStdHandle(STD_OUTPUT_HANDLE), fmtW, V_BSTR( &v ) ); output_line( V_BSTR( &v ) );
VariantClear( &v ); VariantClear( &v );
} }
IWbemClassObject_Release( obj ); IWbemClassObject_Release( obj );