Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HKCU\Console\CodePage ... does it do anything? #11591

Closed
vefatica opened this issue Oct 23, 2021 · 13 comments
Closed

HKCU\Console\CodePage ... does it do anything? #11591

vefatica opened this issue Oct 23, 2021 · 13 comments
Labels
Issue-Question For questions or discussion Product-Conpty For console issues specifically related to conpty Resolution-Duplicate There's another issue on the tracker that's pretty much the same thing.

Comments

@vefatica
Copy link

Microsoft Windows 10 Pro for Workstations
10.0.19043.1288 (2009, 21H1)

I thought that HKCU\Console\CodePage set the default output code page for consoles (and I'm pretty sure that, at least once upon a time, it did). But it does not now. Although I have

hkcu\console
  CodePage : REG_DWORD : 1252

all console apps which don't have another specification start with CP 437 (CP_OEMCP).

But if, for example, I have

hkcu\console\SystemRoot_system32_cmd.exe
    CodePage : REG_DWORD : 1252

then CMD will start with CP 1252.

Shouldn't HKCU\Console\CodePage be setting the default?

And (apparently unrelated to the above) how do I get all shells to use output code page 1252 when running in WindowsTerminal? They all seem to get 437.

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Oct 23, 2021
@j4james
Copy link
Collaborator

j4james commented Oct 24, 2021

I just happened to be looking at this area of the code recently, so I have a general idea of how it works, and this looks like a bug to me. The place that the settings are initialized at startup is in the SetUpConsole function. You can see the relevant bit here:

reg.LoadDefaultFromRegistry();
// 2. Read specific settings
// Link is expecting the flags from the process to be in already, so apply that first
settings.SetStartupFlags(pStartupSettings->GetStartupFlags());
// We need to see if we were spawned from a link. If we were, we need to
// call back into the shell to try to get all the console information from the link.
ServiceLocator::LocateSystemConfigurationProvider()->GetSettingsFromLink(&settings, Title, &TitleLength, CurDir, AppName);
// If we weren't started from a link, this will already be set.
// If LoadLinkInfo couldn't find anything, it will remove the flag so we can dig in the registry.
if (!(settings.IsStartupTitleIsLinkNameSet()))
{
reg.LoadFromRegistry(Title);
}

Note that it loads the setting from the default registry area first, then potentially overrides that with settings in the shortcut link, and if there was no shortcut link, it'll look again in the registry for settings specific to the current executable.

The problem, though, is that the SystemConfigurationProvider::GetSettingsFromLink method always resets the codepage to the OEM value, regardless of whether a shortcut link is even involved, so any value read from the default registry area is guaranteed to be reset. See here:

void SystemConfigurationProvider::GetSettingsFromLink(
_Inout_ Settings* pLinkSettings,
_Inout_updates_bytes_(*pdwTitleLength) LPWSTR pwszTitle,
_Inout_ PDWORD pdwTitleLength,
_In_ PCWSTR pwszCurrDir,
_In_ PCWSTR pwszAppName)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
WCHAR wszLinkTarget[MAX_PATH] = { 0 };
WCHAR wszIconLocation[MAX_PATH] = { 0 };
int iIconIndex = 0;
pLinkSettings->SetCodePage(ServiceLocator::LocateGlobals().uiOEMCP);

I can't see any reason why that line is necessary, and removing it seems to fix the problem. But maybe one of the core devs will have access to the git history going further back and can see why it was added. It's possible there's a legitimate reason for it.

@j4james
Copy link
Collaborator

j4james commented Oct 24, 2021

And (apparently unrelated to the above) how do I get all shells to use output code page 1252 when running in WindowsTerminal? They all seem to get 437.

This seems to be by design. When conhost is started in a conpty session it deliberately doesn't initialize settings from the registry. See here:

// Use the launch arguments to check if we're going to be started in pseudoconsole mode.
// If we are, we don't want to load any user settings, because that could
// result in some strange rendering results in the end terminal.
// Use the launch args because the VtIo hasn't been initialized yet.
if (!launchArgs.InConptyMode())
{
// 3. Read the default registry values.
Registry reg(&settings);

@eryksun
Copy link

eryksun commented Oct 24, 2021

I can't see any reason why that line is necessary, and removing it seems to fix the problem. But maybe one of the core devs will have access to the git history going further back and can see why it was added.

If I recall correctly, the default "CodePage" setting was ignored in Windows 7. It definitely worked in Windows 2000, which I think was the first version of Windows that supported it. I verified this in Windows 2000 running in a VM.

When conhost is started in a conpty session it deliberately doesn't initialize settings from the registry.

A pair of functions could be added to the API to get and set the classic console settings that are applicable in the pseudoconsole context, such as "CodePage" and "NumberOfHistoryBuffers". For example: GetPseudoConsoleConfig(HPCON, PPSEUDO_CONSOLE_CONFIG) and SetPseudoConsoleConfig(HPCON, PPSEUDO_CONSOLE_CONFIG). The "Settings" tab in Windows Terminal could have a page to modify the default pseudoconsole settings (not the defaults in the registry), which each profile would be able to override.

@j4james
Copy link
Collaborator

j4james commented Oct 24, 2021

If you're wanting a setting in Windows Terminal to specify the initial codepage for a profile, I think that is similar to what was requested in #9174 and/or #10870. It's also been discussed tangentially in #10408 (comment).

@vefatica
Copy link
Author

If you're wanting a setting in Windows Terminal to specify the initial codepage for a profile, I think that is similar to what was requested in #9174 and/or #10870. It's also been discussed tangentially in #10408 (comment).

I'd be happy with setting it for all profiles. If I read you correctly, @j4james, that's not currently possible. The user should have some say ... yes/no?

@j4james
Copy link
Collaborator

j4james commented Oct 24, 2021

Well the user does have some say. They can change the codepage with chcp. And if you're in a WSL shell you can at least change to something similar to 1252 with with echo -e '\e%@', and change back to UTF-8 with echo -e '\e%G'.

But Windows Terminal doesn't currently have any control over that (as far as I'm aware), because it's all handled in conhost. What gets passed to conpty (which is what Windows Terminal sees), is a simple stream of UTF-8 after everything has already been translated.

But if this is something you need to control from within Windows Terminal itself, I'd suggest upvoting #10870, which I believe is requesting the same thing. If you have a compelling use case for it, maybe explain what that is in a comment.

@vefatica
Copy link
Author

When I started this thread, I was thinking Windows should be honoring the default code page setting. The user should have some across-the-board control in consoles and in WT. I upvoted #10870 but didn't leave a use case. Today's compelling reason is that I prefer the first (below) to the second. :-)

v:\> chcp 1252 & type V:\wmips.btm | findstr ZZZ
Active code page: 1252
:: rewrite YYYYMMDDHHMMSS.mmmmmm±ZZZ

v:\> chcp 437 & type V:\wmips.btm | findstr ZZZ
Active code page: 437
:: rewrite YYYYMMDDHHMMSS.mmmmmm▒ZZZ

@vefatica
Copy link
Author

The pasted text in my last post looks peculiar in MSEGDE. It should look like this.

image

@j4james
Copy link
Collaborator

j4james commented Oct 24, 2021

If you're using a cmd shell, you could just change the command line in your Windows Terminal profile to something like this: cmd.exe /k chcp 1252

@vefatica
Copy link
Author

If you're using a cmd shell, you could just change the command line in your Windows Terminal profile to something like this: cmd.exe /k chcp 1252

... or use CMD's AutoRun file. I don't use CMD often. For any command interpreter, there are specific work-arounds.

@zadjii-msft
Copy link
Member

You know, I thought our plan of record was to actually kinda go in the opposite direction, and have conpty force itself into utf-8 mode by default - #1802. Of course, we never really achieved consensus on that one, and I don't think any of us thought it was a terribly high-priority issue.

@zadjii-msft zadjii-msft added Issue-Question For questions or discussion Product-Conpty For console issues specifically related to conpty labels Nov 3, 2021
@carlos-zamora
Copy link
Member

Thanks for filing. We think the right solution to this is going to be allow the user to set the codepage in a compatibility page of the settings UI. This work spans several issues: #10870, #9174, #15678, and #1802. So we're going to mark this as a /dup of #1802, even though it's not necessarily the perfect solution to this question, but we'll use it to do the right thing.

@microsoft-github-policy-service
Copy link
Contributor

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-Duplicate There's another issue on the tracker that's pretty much the same thing. and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question For questions or discussion Product-Conpty For console issues specifically related to conpty Resolution-Duplicate There's another issue on the tracker that's pretty much the same thing.
Projects
None yet
Development

No branches or pull requests

5 participants