ReactOS 0.4.15-dev-8102-g108db8f
chcp.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <windef.h>
#include <winbase.h>
#include <wincon.h>
#include <conutils.h>
#include "resource.h"
Include dependency graph for chcp.c:

Go to the source code of this file.

Functions

int wmain (int argc, WCHAR *argv[])
 

Function Documentation

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

IMPORTANT NOTE: This code must be kept synchronized with MODE.COM!SetConsoleCPState()

Definition at line 30 of file chcp.c.

31{
32 UINT uOldCodePage, uNewCodePage;
33
34 /* Initialize the Console Standard Streams */
36
37 /* Print help */
38 if (argc > 1 && wcscmp(argv[1], L"/?") == 0)
39 {
41 return 0;
42 }
43
44 if (argc == 1)
45 {
46 /* Display the active code page number */
48 return 0;
49 }
50
51 if (argc > 2)
52 {
53 /* Too many parameters */
55 return 1;
56 }
57
58 uNewCodePage = (UINT)_wtoi(argv[1]);
59
60 if (uNewCodePage == 0)
61 {
63 return 1;
64 }
65
70 /*
71 * Save the original console code page to be restored
72 * in case SetConsoleCP() or SetConsoleOutputCP() fails.
73 */
74 uOldCodePage = GetConsoleCP();
75
76 /*
77 * Try changing the console input and output code pages.
78 * If it succeeds, refresh the local code page information.
79 */
80 if (SetConsoleCP(uNewCodePage))
81 {
82 if (SetConsoleOutputCP(uNewCodePage))
83 {
84 /* Success, reset the current thread UI language
85 * and update the streams cached code page. */
87 ConStdStreamsSetCacheCodePage(uNewCodePage, uNewCodePage);
88
89 /* Display the active code page number */
91 return 0;
92 }
93 else
94 {
95 /* Failure, restore the original console code page */
96 SetConsoleCP(uOldCodePage);
97 }
98 }
99
100 /* An error happened, display an error and bail out */
102 return 1;
103}
static int argc
Definition: ServiceArgs.c:12
#define STRING_ERROR_INVALID_PARAM_FORMAT
Definition: resource.h:5
#define STRING_CHCP_ERROR4
Definition: resource.h:8
#define STRING_CHCP_ERROR1
Definition: resource.h:7
#define STRING_CHCP_HELP
Definition: resource.h:10
#define ConInitStdStreams()
Definition: fc.c:13
#define StdOut
Definition: fc.c:14
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
#define StdErr
Definition: fc.c:15
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
BOOL WINAPI SetConsoleOutputCP(IN UINT wCodepage)
Definition: console.c:695
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(VOID)
Definition: console.c:2451
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCP(UINT wCodePageID)
Definition: console.c:2420
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(VOID)
Definition: console.c:2391
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
#define argv
Definition: mplay32.c:18
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define ConStdStreamsSetCacheCodePage(InputCodePage, OutputCodePage)
Definition: stream.h:152
LANGID ConSetThreadUILanguage(IN LANGID LangId OPTIONAL)
Definition: utils.c:352