From feb9ba9a7fce11b0f776e7f8ccb01e809c78aec0 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Tue, 4 Nov 2025 22:33:19 +0100 Subject: [PATCH] [NETSH] Implement and use the PrintError function --- base/applications/network/netsh/interpreter.c | 2 +- base/applications/network/netsh/lang/en-US.rc | 8 +- base/applications/network/netsh/netsh.c | 77 ++++++++++++++++++- base/applications/network/netsh/netsh.rc | 1 + base/applications/network/netsh/precomp.h | 2 + base/applications/network/netsh/resource.h | 8 +- 6 files changed, 88 insertions(+), 10 deletions(-) diff --git a/base/applications/network/netsh/interpreter.c b/base/applications/network/netsh/interpreter.c index d5503fc7592..1374eb029e7 100644 --- a/base/applications/network/netsh/interpreter.c +++ b/base/applications/network/netsh/interpreter.c @@ -427,7 +427,7 @@ InterpretInteractive(VOID) if (dwError == ERROR_CMD_NOT_FOUND) { PWSTR pszCommandString = MergeStrings(args_vector, dwArgCount); - ConResPrintf(StdErr, IDS_INVALID_COMMAND, pszCommandString); + PrintError(NULL, dwError, pszCommandString); HeapFree(GetProcessHeap(), 0, pszCommandString); } diff --git a/base/applications/network/netsh/lang/en-US.rc b/base/applications/network/netsh/lang/en-US.rc index 8cdaac1f666..82eb67f8756 100644 --- a/base/applications/network/netsh/lang/en-US.rc +++ b/base/applications/network/netsh/lang/en-US.rc @@ -12,9 +12,7 @@ STRINGTABLE BEGIN IDS_APP_USAGE "\nUsage: netsh [-a AliasFile] [-c Context] [-r RemoteMachine] \ \n [Command | -f ScriptFile]\n" - IDS_INVALID_COMMAND "The following command was not found: %ls.\n" IDS_OPEN_FAILED "The file %ls could not be openend.\n" - IDS_INVALID_SYNTAX "The syntax supplied for this command is not valid. Check help for the correct syntax.\n\n" IDS_THIS_COMMANDS "\nCommands in this context:\n" IDS_CONTEXT_COMMANDS "\nCommands in the %s-context:\n" END @@ -76,3 +74,9 @@ BEGIN IDS_HLP_GROUP_SET "Updates configuration settings.\n" IDS_HLP_GROUP_SHOW "Displays information.\n" END + +STRINGTABLE +BEGIN + ERROR_INVALID_SYNTAX "The syntax supplied for this command is not valid. Check help for the correct syntax.\n" + ERROR_CMD_NOT_FOUND "The following command was not found: %1!s!.\n" +END diff --git a/base/applications/network/netsh/netsh.c b/base/applications/network/netsh/netsh.c index a6f4785556d..cf653042f02 100644 --- a/base/applications/network/netsh/netsh.c +++ b/base/applications/network/netsh/netsh.c @@ -383,8 +383,81 @@ PrintError( _In_ DWORD dwErrId, ...) { - DPRINT1("PrintError()\n"); - return 1; + PWSTR pszInBuffer = NULL, pszOutBuffer = NULL; + DWORD dwLength = 0; + va_list ap; + + DPRINT("PrintError(%p %lu ...)\n", hModule, dwErrId); + + va_start(ap, dwErrId); + + pszOutBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_BUFFER_SIZE * sizeof(WCHAR)); + if (pszOutBuffer == NULL) + goto done; + + if (hModule) + { + pszInBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_BUFFER_SIZE * sizeof(WCHAR)); + if (pszInBuffer == NULL) + goto done; + + dwLength = LoadStringW(hModule, dwErrId, pszInBuffer, HUGE_BUFFER_SIZE); + if (dwLength == 0) + goto done; + + dwLength = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, + pszInBuffer, + 0, + 0, + pszOutBuffer, + HUGE_BUFFER_SIZE, + &ap); + } + else + { + if ((dwErrId > NETSH_ERROR_BASE) && (dwErrId < NETSH_ERROR_END)) + { + pszInBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_BUFFER_SIZE * sizeof(WCHAR)); + if (pszInBuffer == NULL) + goto done; + + dwLength = LoadStringW(GetModuleHandle(NULL), dwErrId, pszInBuffer, HUGE_BUFFER_SIZE); + if (dwLength == 0) + goto done; + + dwLength = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, + pszInBuffer, + 0, + 0L, + pszOutBuffer, + HUGE_BUFFER_SIZE, + &ap); + } + else + { + dwLength = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + dwErrId, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + pszOutBuffer, + HUGE_BUFFER_SIZE, + &ap); + } + } + + va_end(ap); + + if (dwLength > 0) + ConPuts(StdOut, pszOutBuffer); + +done: + if (pszOutBuffer) + HeapFree(GetProcessHeap(), 0, pszOutBuffer); + + if (pszInBuffer) + HeapFree(GetProcessHeap(), 0, pszInBuffer); + + return dwLength; } DWORD diff --git a/base/applications/network/netsh/netsh.rc b/base/applications/network/netsh/netsh.rc index 448c3239e04..206c99e3f9a 100644 --- a/base/applications/network/netsh/netsh.rc +++ b/base/applications/network/netsh/netsh.rc @@ -1,4 +1,5 @@ #include +#include #include "resource.h" diff --git a/base/applications/network/netsh/precomp.h b/base/applications/network/netsh/precomp.h index ef2162c6638..2272c00ac22 100644 --- a/base/applications/network/netsh/precomp.h +++ b/base/applications/network/netsh/precomp.h @@ -35,6 +35,8 @@ /* DEFINES *******************************************************************/ +#define HUGE_BUFFER_SIZE 2048 + #define MAX_STRING_SIZE 1024 #define MAX_ARGS_COUNT 256 diff --git a/base/applications/network/netsh/resource.h b/base/applications/network/netsh/resource.h index 5cce164fddb..3be0cf7de39 100644 --- a/base/applications/network/netsh/resource.h +++ b/base/applications/network/netsh/resource.h @@ -11,11 +11,9 @@ #define IDS_APP_USAGE 100 #define IDS_APP_PROMPT 101 -#define IDS_INVALID_COMMAND 102 -#define IDS_OPEN_FAILED 103 -#define IDS_INVALID_SYNTAX 104 -#define IDS_THIS_COMMANDS 105 -#define IDS_CONTEXT_COMMANDS 106 +#define IDS_OPEN_FAILED 102 +#define IDS_THIS_COMMANDS 103 +#define IDS_CONTEXT_COMMANDS 104 #define IDS_HELP_HEADER 200 #define IDS_HELP_FOOTER 201