[NETSH] Implement and use the PrintError function

This commit is contained in:
Eric Kohl
2025-11-04 22:33:19 +01:00
parent 2a9a0d0438
commit feb9ba9a7f
6 changed files with 88 additions and 10 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +1,5 @@
#include <windef.h>
#include <netsh.h>
#include "resource.h"

View File

@@ -35,6 +35,8 @@
/* DEFINES *******************************************************************/
#define HUGE_BUFFER_SIZE 2048
#define MAX_STRING_SIZE 1024
#define MAX_ARGS_COUNT 256

View File

@@ -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