Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

OpenConsole.exe vs conhost.exe, questions #8101

Closed
vefatica opened this issue Oct 29, 2020 · 13 comments
Closed

OpenConsole.exe vs conhost.exe, questions #8101

vefatica opened this issue Oct 29, 2020 · 13 comments
Labels
Issue-Question For questions or discussion Needs-Tag-Fix Doesn't match tag requirements Resolution-Answered Related to questions that have been answered

Comments

@vefatica
Copy link

I stumbled on a thread here where it was pointed out that OpenConsole.exe could replace conhost.exe. While I did not replace conhost.exe, I found that is was easy enough to start a console app in OpenConsole instead of conhost. I've simply used my favorite shell ... "openconsole.exe d:\tc26\tcc.exe". And that gives me many questions.

Is there a place where I can read about command line arguments for OpenConsole and/or the differences/similarities between OpenConsole and conhost? I noticed that OpenConsole reads HKCU\Console. Can I get it to read a subkey of HKCU\Console?

One noticeable difference is that after setting custom tabstops (usingTBC and HTS), those settings are lost if I "CMD /C" while using conhost.exe (first pic below), and they're not lost if I do the same thing while using OpenConsole (second pic below). What's the difference there. ["CMD /C" is nothing special. There are many ways I can clobber the tab settings in conhost and not in OpenConsole.] Thanks in advance. - Vince

Conhost:

image

OpenConsole:

image

@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 29, 2020
@DHowett DHowett added the Issue-Question For questions or discussion label Oct 29, 2020
@zadjii-msft
Copy link
Member

Fundamentally, they're the same thing. When the code in this repo for openconsole.exe is built by the Windows build system, it turns into conhost.exe. Really, the only difference you're gonna see between the two is that the OpenConsole in this repo (and that ships with the Terminal) has a few more months of bug fixes and features added to it relative to the conhost.exe in your system32. If someone's interested, I could give a history lesson on why the two are named differently, but that's more of an aside.

We're explicitly not documenting the arguments to conhost, because then 3rd party developers could take a dependency on those arguments, and we would have a lot less flexibility in changing them in the future. Generally, the arguments only exist to help support conpty scenarios. If we really needed to change them in the future, we like keeping the door open for that. The astute reader could go looking through this repo to find the args to conhost if they really wanted, but again, there's no contract here - those are subject to change at any time.

As far as the tab stops thing - what version of Windows are you on? We had a change a while ago where turning VT support off and on would reset the tabstops set by VT. And for reasons that I don't want to get into the weeds on, cmd.exe will disable VT support when launching a child process, then re-enable it when it starts. So doing cmd.exe will probably cause VT to get toggled, resetting the tab stops. I'm sure I could go look up the actual issue # for where we changed the tabstop behavior - I'm pretty confident it was after we started using this repo to track issues.

@zadjii-msft zadjii-msft added the Resolution-Answered Related to questions that have been answered label Oct 30, 2020
@vefatica
Copy link
Author

Thanks for your response, Mike.

As for the tab thing, as I said, cmd.exe is not special. I'm pretty sure it's my shell doing what cmd.exe does ... turn VT processing off/on in various scenarios. I see evidence of it when starting just about anything, including pipe instances. Making the tab settings "static" is a nice touch (and suits me just fine).

I found the command line args in the source (nothing very interesting to me). When I "openconsole.exe myshell.exe", it's not same as Windows using conhost.exe. Openconsole.exe becomes the parent of myshell.exe, an apppropriate subkey of HKCU\Console is not read (if it exists), the console does not automatically display myshell's icon, and probably more differences I'm yet to discover. From your remarks I suppose that would change if Windows actually used openconsole.exe. Short of actually replacing conhost.exe with openconsole.exe (I've read several "no guarantees" from you (Mike) and Dustin) is there a way to mimmick running an app in openconsole.exe the same way that Windows runs an app in conhost.exe?

@vefatica
Copy link
Author

You asked and I neglected to reply. I'm on

Microsoft Windows 10 Pro for Workstations
10.0.18363.1139 (1909)

Should I expect conhost.exe to reset the tabstops when VT is toggled?

@zadjii-msft
Copy link
Member

So technically, yes, there's a way to do it, but I'm not positive that this counts as supported. So HERE BE DRAGONS.

Technically, what happens when the OS determines that you're launching a console application that doesn't currently have a console server, it creates a new conhost for that process, and aclls some private APIs to re-parent the commandline application as a child of conhost.exe. In order to connect the client to the conhost.exe it's about to spawn, it creates a bunch of console handles in ConDrv, and passes the server handle to conhost on the commandline. That's why you'll see the commandline for conhost typically be conhost.exe 0x4 - 0x4 is the value of the console server handle that the OS has created for this console instance.

You know what, now that I've typed this all up, I'm pretty sure there isn't a way to do this publicly. That private API, the one that re-parents a process, that's probably the one you really want to be able to call. I'm pretty confident that we won't be able to make that one publicly available.

Running openconsole myapp.exe was really only ever intended as a quick-and-dirty way to iterate quickly on console features, without needing to wait for an entire windows build. It's a happy coincidence that it has worked so well for us to be able to use it as a side-by-side conpty host for the Terminal. I'd again caution against using it in production 😃


As far as the tabstops thing - I asked for your version number, but I honestly have no recollection of when that was actually changed. #140 looks like it's about what we're looking for, but then that would have shipped in 1809, so that's not right. Then there was #4669/#5173, which is a good amount more recent. Judging only based off of bot comments, this one hasn't made it to an Insider's build yet, but that can't be right - @DHowett did the bot miss a bunch of commits when we just bulk merged a bunch of stuff to OS>

@zadjii-msft zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Nov 2, 2020
@DHowett
Copy link
Member

DHowett commented Nov 2, 2020

The bot that comments on insider build merges is me :) and this was one of the earlier fixes we made, so I almost certainly didn’t have a process for tracking them back then. I believe James fixed tab stops?

@vefatica
Copy link
Author

vefatica commented Nov 2, 2020

Thanks again. Sometime fairly recently I read about a re-parenting hack but that's not sufficiently important to make me look for it again. I was more interested in the convenience of getting OpenConsole automatically. Do you want reports of differences between OpenConsole and Conhost ... even when Conhost seems to have the edge? Two are pretty obvious.

  1. Conhost sets the icon while OpenConsole doesn't.
  2. I use the dark theme in Windows 10. Conhost has dark scrollbars; OpenConsole has light ones.

image

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Nov 2, 2020
@DHowett
Copy link
Member

DHowett commented Nov 2, 2020

I would go so far as to say: no, we do not want such reports. They are built out of the same code (delta a couple private APIs). OpenConsole is not a shipping production scenario and we will not be changing it to address any perceived differences from conhost.

@DHowett
Copy link
Member

DHowett commented Nov 2, 2020

(The icon/registry key/shortcut detection is broken because OpenConsole isn’t receiving the LPSTARTUPINFO that the erstwhile parent process started with... because it isn’t being spawned in response to somebody else needing a console handle.)

@DHowett
Copy link
Member

DHowett commented Nov 2, 2020

If you want to get OpenConsole automatically, replace conhost on your system. No warranty is expressed or implied. Right now, it’s the only way.

Eventually, the spec in #7414 will land and we’ll agree on how to seamlessly replace conhost with some other console host.

@eryksun
Copy link

eryksun commented Nov 3, 2020

[calls] some private APIs to re-parent the commandline application as a child of conhost.exe

The conhost.exe process is the child of the allocating process. That has been the case since the ConDrv device was introduced in Windows 8. IIRC, in the old Windows 7 implementation that used ALPC pseudo-files instead of a device, each conhost.exe session was created by the desktop session server, csrss.exe.

From what I remember from the last time I walked through this in a debugger, the console init routine in kernelbase does basically the following steps:

  • open "\Device\ConDrv\Server"
  • open "Reference" relative to the "Server" handle
  • make an ioctl to the "Server" file to pass the parameters for the new conhost.exe server process
  • wait for ConDrv in the kernel to create the session server process
  • open "Connect" relative to the "Reference" handle via NtCreateFile. IIRC, the call passes connection information -- such as the current process STARTUPINFO -- via the EaBuffer argument (extended attributes). The handle for the "Connect" file is the process ConsoleHandle, which is used for miscellaneous console functions such as GetConsoleCP.
  • open the generic "Input" and "Output" files relative to the "Connect" file, and duplicate the handle for the "Output" file. The handles for these files are the process StandardInput, StandardOutput, and StandardError handles
  • set the PID of the conhost.exe server as data in the current process via NtSetInformationProcess: ProcessConsoleHostProcess
  • initialize the console control handler list to the default handler function that calls ExitProcess(STATUS_CONTROL_C_EXIT)

@vefatica
Copy link
Author

vefatica commented Nov 6, 2020

I replaced conhost (briefly). It worked OK but I didn't like looking at the white scroll bar.

@DHowett
Copy link
Member

DHowett commented Nov 6, 2020

Ah, console users can be so picky. 😄

@vefatica
Copy link
Author

vefatica commented Nov 6, 2020

We have been spoiled.

@DHowett DHowett closed this as completed Nov 6, 2020
@DHowett DHowett removed Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Nov 6, 2020
@ghost ghost added the Needs-Tag-Fix Doesn't match tag requirements label Nov 6, 2020
@microsoft microsoft locked and limited conversation to collaborators Jan 7, 2022
@DHowett DHowett converted this issue into discussion #12115 Jan 7, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Issue-Question For questions or discussion Needs-Tag-Fix Doesn't match tag requirements Resolution-Answered Related to questions that have been answered
Projects
None yet
Development

No branches or pull requests

4 participants