2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Change CodePage Command
4 * FILE: base/applications/cmdutils/chcp/chcp.c
5 * PURPOSE: Displays or changes the active console input and output codepages.
6 * PROGRAMMERS: Eric Kohl
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
10 * CHCP.C - chcp internal command.
12 * 23-Dec-1998 (Eric Kohl)
15 * 02-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
16 * Remove all hardcoded strings in En.rc
30 // INT CommandChcp(LPTSTR cmd, LPTSTR param)
31 int wmain(int argc
, WCHAR
* argv
[])
33 UINT uOldCodePage
, uNewCodePage
;
35 /* Initialize the Console Standard Streams */
39 if (argc
> 1 && wcscmp(argv
[1], L
"/?") == 0)
41 ConResPuts(StdOut
, STRING_CHCP_HELP
);
47 /* Display the active code page number */
48 ConResPrintf(StdOut
, STRING_CHCP_ERROR1
, GetConsoleOutputCP());
54 /* Too many parameters */
55 ConResPrintf(StdErr
, STRING_ERROR_INVALID_PARAM_FORMAT
, argv
[2]);
59 uNewCodePage
= (UINT
)_wtoi(argv
[1]);
61 if (uNewCodePage
== 0)
63 ConResPrintf(StdErr
, STRING_ERROR_INVALID_PARAM_FORMAT
, argv
[1]);
68 * Save the original console codepage to be restored in case
69 * SetConsoleCP() or SetConsoleOutputCP() fails.
71 uOldCodePage
= GetConsoleCP();
74 * Try changing the console input codepage. If it works then also change
75 * the console output codepage, and refresh our local codepage cache.
77 if (SetConsoleCP(uNewCodePage
))
79 if (SetConsoleOutputCP(uNewCodePage
))
81 /* Display the active code page number */
82 ConResPrintf(StdOut
, STRING_CHCP_ERROR1
, GetConsoleOutputCP());
87 /* Failure, restore the original console codepage */
88 SetConsoleCP(uOldCodePage
);
92 /* An error happened, display an error and bail out */
93 ConResPuts(StdErr
, STRING_CHCP_ERROR4
);