How to connect to a child conhost.exe process of an external application

Question:

I’m trying to communicate with an interactive Windows Console application, to send commands to it and receive their output, but the subprocess the create_subprocess_exec() catches seems to be the wrong process, as the application creates two processes.

When starting the subprocess, two processes open – JoyShockMapper.exe and conhost.exe. As far as I understand, conhost.exe is the actual console I should be communicating with which talks to JoyShockMapper.exe, because I fail to properly communicate with proc.
By getting the PID and searching for it I can see, that this is actually the subprocess of the JoyShockMapper.exe and not the conhost.exe. How can I access the conhost.exe process instead?

EDIT: After trying to enter commands into the console application and monitoring the task manager, the memory value of JoyShockMapper.exe process changes, but conhost.exe doesn’t change at all. Would this mean that the JoyShockMapper.exe is the console I’m looking for? If so, am I communicating with it the wrong way? And how do I talk to it properly?

EDIT 2: No, the conhost.exe seems to be the thing I need to talk to. How do I connect to it?

import asyncio
from time import sleep


async def main():
    proc = await asyncio.subprocess.create_subprocess_exec(
        "JoyShockMapper.exe",
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE)
    sleep(3)
    print(proc.pid) # Outputs JoyShockMapper.exe PID
    proc.stdin.write(b"HIDE_MINIMIZED = OFFn") # Doesn't do anything, no change in the console
    print(await proc.stdout.read(1024)) # Outputs b''
    await proc.wait()


asyncio.run(main())
Asked By: Ivan Yermakov

||

Answers:

There is no fix.

After I posted this question I’ve did some more research and I’m pretty sure I saw somewhere Microsoft stating that it’s impossible to connect to a running application’s stdin and stdout outside of the code that has launched it (JSM’s code in this case).

I’ve decided to rather find the PID of the console host by checking for a match in it’s parent PID and doing some hacky awful stuff to send keys to it’s window for commands. Getting output from it is unfortunately impossible on Windows.

Answered By: Ivan Yermakov
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.