Skip to content

Commit feb9ba9

Browse files
committed
[NETSH] Implement and use the PrintError function
1 parent 2a9a0d0 commit feb9ba9

File tree

6 files changed

+88
-10
lines changed

6 files changed

+88
-10
lines changed

base/applications/network/netsh/interpreter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ InterpretInteractive(VOID)
427427
if (dwError == ERROR_CMD_NOT_FOUND)
428428
{
429429
PWSTR pszCommandString = MergeStrings(args_vector, dwArgCount);
430-
ConResPrintf(StdErr, IDS_INVALID_COMMAND, pszCommandString);
430+
PrintError(NULL, dwError, pszCommandString);
431431
HeapFree(GetProcessHeap(), 0, pszCommandString);
432432
}
433433

base/applications/network/netsh/lang/en-US.rc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ STRINGTABLE
1212
BEGIN
1313
IDS_APP_USAGE "\nUsage: netsh [-a AliasFile] [-c Context] [-r RemoteMachine] \
1414
\n [Command | -f ScriptFile]\n"
15-
IDS_INVALID_COMMAND "The following command was not found: %ls.\n"
1615
IDS_OPEN_FAILED "The file %ls could not be openend.\n"
17-
IDS_INVALID_SYNTAX "The syntax supplied for this command is not valid. Check help for the correct syntax.\n\n"
1816
IDS_THIS_COMMANDS "\nCommands in this context:\n"
1917
IDS_CONTEXT_COMMANDS "\nCommands in the %s-context:\n"
2018
END
@@ -76,3 +74,9 @@ BEGIN
7674
IDS_HLP_GROUP_SET "Updates configuration settings.\n"
7775
IDS_HLP_GROUP_SHOW "Displays information.\n"
7876
END
77+
78+
STRINGTABLE
79+
BEGIN
80+
ERROR_INVALID_SYNTAX "The syntax supplied for this command is not valid. Check help for the correct syntax.\n"
81+
ERROR_CMD_NOT_FOUND "The following command was not found: %1!s!.\n"
82+
END

base/applications/network/netsh/netsh.c

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,81 @@ PrintError(
383383
_In_ DWORD dwErrId,
384384
...)
385385
{
386-
DPRINT1("PrintError()\n");
387-
return 1;
386+
PWSTR pszInBuffer = NULL, pszOutBuffer = NULL;
387+
DWORD dwLength = 0;
388+
va_list ap;
389+
390+
DPRINT("PrintError(%p %lu ...)\n", hModule, dwErrId);
391+
392+
va_start(ap, dwErrId);
393+
394+
pszOutBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_BUFFER_SIZE * sizeof(WCHAR));
395+
if (pszOutBuffer == NULL)
396+
goto done;
397+
398+
if (hModule)
399+
{
400+
pszInBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_BUFFER_SIZE * sizeof(WCHAR));
401+
if (pszInBuffer == NULL)
402+
goto done;
403+
404+
dwLength = LoadStringW(hModule, dwErrId, pszInBuffer, HUGE_BUFFER_SIZE);
405+
if (dwLength == 0)
406+
goto done;
407+
408+
dwLength = FormatMessageW(FORMAT_MESSAGE_FROM_STRING,
409+
pszInBuffer,
410+
0,
411+
0,
412+
pszOutBuffer,
413+
HUGE_BUFFER_SIZE,
414+
&ap);
415+
}
416+
else
417+
{
418+
if ((dwErrId > NETSH_ERROR_BASE) && (dwErrId < NETSH_ERROR_END))
419+
{
420+
pszInBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_BUFFER_SIZE * sizeof(WCHAR));
421+
if (pszInBuffer == NULL)
422+
goto done;
423+
424+
dwLength = LoadStringW(GetModuleHandle(NULL), dwErrId, pszInBuffer, HUGE_BUFFER_SIZE);
425+
if (dwLength == 0)
426+
goto done;
427+
428+
dwLength = FormatMessageW(FORMAT_MESSAGE_FROM_STRING,
429+
pszInBuffer,
430+
0,
431+
0L,
432+
pszOutBuffer,
433+
HUGE_BUFFER_SIZE,
434+
&ap);
435+
}
436+
else
437+
{
438+
dwLength = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
439+
NULL,
440+
dwErrId,
441+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
442+
pszOutBuffer,
443+
HUGE_BUFFER_SIZE,
444+
&ap);
445+
}
446+
}
447+
448+
va_end(ap);
449+
450+
if (dwLength > 0)
451+
ConPuts(StdOut, pszOutBuffer);
452+
453+
done:
454+
if (pszOutBuffer)
455+
HeapFree(GetProcessHeap(), 0, pszOutBuffer);
456+
457+
if (pszInBuffer)
458+
HeapFree(GetProcessHeap(), 0, pszInBuffer);
459+
460+
return dwLength;
388461
}
389462

390463
DWORD

base/applications/network/netsh/netsh.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <windef.h>
2+
#include <netsh.h>
23

34
#include "resource.h"
45

base/applications/network/netsh/precomp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
/* DEFINES *******************************************************************/
3737

38+
#define HUGE_BUFFER_SIZE 2048
39+
3840
#define MAX_STRING_SIZE 1024
3941
#define MAX_ARGS_COUNT 256
4042

base/applications/network/netsh/resource.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
#define IDS_APP_USAGE 100
1313
#define IDS_APP_PROMPT 101
14-
#define IDS_INVALID_COMMAND 102
15-
#define IDS_OPEN_FAILED 103
16-
#define IDS_INVALID_SYNTAX 104
17-
#define IDS_THIS_COMMANDS 105
18-
#define IDS_CONTEXT_COMMANDS 106
14+
#define IDS_OPEN_FAILED 102
15+
#define IDS_THIS_COMMANDS 103
16+
#define IDS_CONTEXT_COMMANDS 104
1917

2018
#define IDS_HELP_HEADER 200
2119
#define IDS_HELP_FOOTER 201

0 commit comments

Comments
 (0)