From: Hugh McMaster Subject: wmic: Remove redundant codepath and replace with FormatMessage Message-Id: Date: Tue, 3 Mar 2015 16:01:05 +1100 Removes the redundant vsprintfW codepath and replaces it with FormatMessage. Precedent for these changes: attrib:      6aacb090f6bad8f3109cea710cc3aab462eb97d4 ipconfig:  66b06a499d97338dc5ef0b5c4c78b107d577ced8 net:         f3f24fb5d4b017e026333c5d00066d727741a4b5 xcopy:     0cdabdd64873439052b3efa84850bc08cacf5395 From a6d839cedce15343a3cb6692d98ea74df38b54e5 Mon Sep 17 00:00:00 2001 From: Hugh McMaster Date: Tue, 3 Mar 2015 14:58:33 +1100 Subject: wmic: Remove redundant codepath and replace with FormatMessage --- programs/wmic/main.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/programs/wmic/main.c b/programs/wmic/main.c index 5aee4c1..7afe620 100644 --- a/programs/wmic/main.c +++ b/programs/wmic/main.c @@ -126,13 +126,13 @@ static int output_string( const WCHAR *msg, ... ) va_list va_args; int wlen; DWORD count, ret; - WCHAR buffer[8192]; + WCHAR *buffer; va_start( va_args, msg ); - vsprintfW( buffer, msg, va_args ); + wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, + msg, 0, 0, (WCHAR *)&buffer, 0, &va_args); va_end( va_args ); - wlen = strlenW( buffer ); ret = WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), buffer, wlen, &count, NULL ); if (!ret) { @@ -144,18 +144,24 @@ static int output_string( const WCHAR *msg, ... ) * one in that case. */ len = WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, NULL, 0, NULL, NULL ); - if (!(msgA = HeapAlloc( GetProcessHeap(), 0, len * sizeof(char) ))) return 0; + if (!(msgA = HeapAlloc( GetProcessHeap(), 0, len * sizeof(char) ))) + { + LocalFree(buffer); + return 0; + } WideCharToMultiByte( GetConsoleOutputCP(), 0, buffer, wlen, msgA, len, NULL, NULL ); WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE ); HeapFree( GetProcessHeap(), 0, msgA ); } + LocalFree(buffer); + return count; } static int output_message( int msg ) { - static const WCHAR fmtW[] = {'%','s',0}; + static const WCHAR fmtW[] = {'%','1',0}; WCHAR buffer[8192]; LoadStringW( GetModuleHandleW(NULL), msg, buffer, sizeof(buffer)/sizeof(WCHAR) ); @@ -168,7 +174,7 @@ static int query_prop( const WCHAR *alias, const WCHAR *propname ) 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 newlineW[] = {'\n',0}; - static const WCHAR fmtW[] = {'%','s','\n',0}; + static const WCHAR fmtW[] = {'%','1','\n',0}; HRESULT hr; IWbemLocator *locator = NULL; IWbemServices *services = NULL; -- 1.9.1