[CHCP] Reset the current thread UI language and streams codepage after changing the console codepage.

CORE-17601
This commit is contained in:
Hermès Bélusca-Maïto 2021-05-25 20:04:01 +02:00
parent 7c3aabc088
commit a8ef85ad71
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -1,10 +1,9 @@
/* /*
* COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS Change CodePage Command
* PROJECT: ReactOS Change CodePage Command * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* FILE: base/applications/cmdutils/chcp/chcp.c * PURPOSE: Displays or changes the active console input and output code pages.
* PURPOSE: Displays or changes the active console input and output codepages. * COPYRIGHT: Copyright 1999 Eric Kohl
* PROGRAMMERS: Eric Kohl * Copyright 2017-2021 Hermes Belusca-Maito
* Hermes Belusca-Maito (hermes.belusca@sfr.fr)
*/ */
/* /*
* CHCP.C - chcp internal command. * CHCP.C - chcp internal command.
@ -64,27 +63,36 @@ int wmain(int argc, WCHAR* argv[])
return 1; return 1;
} }
/**
** IMPORTANT NOTE: This code must be kept synchronized with MODE.COM!SetConsoleCPState()
**/
/* /*
* Save the original console codepage to be restored in case * Save the original console code page to be restored
* SetConsoleCP() or SetConsoleOutputCP() fails. * in case SetConsoleCP() or SetConsoleOutputCP() fails.
*/ */
uOldCodePage = GetConsoleCP(); uOldCodePage = GetConsoleCP();
/* /*
* Try changing the console input codepage. If it works then also change * Try changing the console input and output code pages.
* the console output codepage, and refresh our local codepage cache. * If it succeeds, refresh the local code page information.
*/ */
if (SetConsoleCP(uNewCodePage)) if (SetConsoleCP(uNewCodePage))
{ {
if (SetConsoleOutputCP(uNewCodePage)) if (SetConsoleOutputCP(uNewCodePage))
{ {
/* Success, reset the current thread UI language
* and update the streams cached code page. */
ConSetThreadUILanguage(0);
ConStdStreamsSetCacheCodePage(uNewCodePage, uNewCodePage);
/* Display the active code page number */ /* Display the active code page number */
ConResPrintf(StdOut, STRING_CHCP_ERROR1, GetConsoleOutputCP()); ConResPrintf(StdOut, STRING_CHCP_ERROR1, GetConsoleOutputCP());
return 0; return 0;
} }
else else
{ {
/* Failure, restore the original console codepage */ /* Failure, restore the original console code page */
SetConsoleCP(uOldCodePage); SetConsoleCP(uOldCodePage);
} }
} }