4

A long time ago I wanted to change the default codepage (CP) of windows console to UTF-8 (to view japanese characters). I can't remember the commands I used, but anyway I eventually managed to be able to view the characters on the cmd.exe. After a short time though, I noticed any programs using the cmd.exe would be in the Japanese codepage 932 by default and OEMCP was set to 932. After noticing this I checked the System Locale and changed it to English (United States). After doing this it was expected that OEMCP would have changed to 437, which it had and which also should have meant the default CP of cmd.exe was now 437. However even after the OEMCP had been changed to 437 the default CP of cmd.exe was still 932.

So how can I change the default CP back to 437? What is causing it to still be CP 932 by default? I have completely removed the Japanese Language from my PC to make sure it wasn't causing the issue, and many people have told me to use an autorun file or change the OEMCP to use CP 437, however OEMCP is already set as 437 and I do not want to use an autorun file for CP 437, I simply want it to be the default as it used to be.

I have also been told that there could be a script that's auto-running each time the cmd.exe is opened, but I have no idea how to track that or how to remove it.

Updates:

The issue is with cmd.exe using CP 932

powershell.exe and netsh.exe are both already using CP 437

user22200
  • 43
  • 5

1 Answers1

5

The default configuration for console windows is stored in the registry key "HKCU\Console". Most of the properties in this key are configurable in the GUI using the console's Alt+Space [D]efaults dialog.

When a process allocates a new console, some of these defaults are directly overridden by the process STARTUPINFO. This includes the window position and size and the screen buffer size and fill attribute (i.e. text and background colors). The process startup information also includes an initial window title, which, if not set by the parent process, defaults to the fully-qualified path of the application executable. If the application is launched using a shell shortcut (i.e. LNK file), the title will be the path to the shortcut file instead of the executable, and the flag STARTF_TITLEISLINKNAME will be set.

The console uses the initial window title to load additional properties that customize the window. If the STARTF_TITLEISLINKNAME flag is set, it loads these additional properties from the LNK shortcut file that launched the application. Otherwise it looks for the normalized title as a subkey in the registry. To normalize the title, backslashes are replaced with underscore, and the Windows directory is replaced by "%SystemRoot%". For example, if the initial window title is "Spam\Eggs", it looks for settings under "HKCU\Console\Spam_Eggs". These properties are configurable in the GUI using the console's Alt+Space [P]roperties dialog.

One of the properties that can be set is a DWORD value named "CodePage". This is the initial legacy codepage for both input and output. (I say "legacy" because the console has a Unicode API that should always be preferred.) If it's not set, the console defaults to the OEM codepage (e.g. 850 in Western Europe or 437 in the U.S.). A LNK shortcut can also set a custom codepage in its ConsoleFEDataBlock, but it's not possible to modify this using the GUI, or even using the IShellLink COM interface.

For example, if cmd.exe is run directly from the Win+R run dialog instead of using a shortcut, it uses the default window title "C:\Windows\System32\cmd.exe". The console in turn, after loading its defaults from "HKCU\Console", looks for additional configuration in the subkey "HKCU\Console\%SystemRoot%_System32_cmd.exe". A "CodePage" value set there will override the console's default OEM codepage.

Unfortunately, if we want to change the initial codepage for all console applications, we'll be disappointed that setting it in "HKCU\Console" doesn't work. Due to a bug in the console (currently implemented by conhostv2.dll, which is hosted in conhost.exe), if we set a default "CodePage" value in "HKCU\Console", the console only briefly sets this value while loading and then resets itself to OEM.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • To set a custom codepage is possible, however. Via [`IShellLinkDataList`](https://learn.microsoft.com/ru-ru/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishelllinkdatalist) interface using [`NT_FE_CONSOLE_PROPS`](https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/ns-shlobj_core-nt_fe_console_props) structure. And [there is an order in which settings are applied to a console window](https://devblogs.microsoft.com/commandline/understanding-windows-console-host-settings/). – Aleksey F. Oct 06 '21 at 20:26