# Windows – Attach a running console application to a new ConEmu instance

conemuconsolewindows

I have an already-started console application, under Windows XP. I want to attach it to a different console emulator such as Console2 or ConEmu. This is possible with ConEmu, and it works if I run ConEmu then use its "Attach to" menu entry and select my console application in the list.

I am now trying to automate this. I want to be able to focus the console application window, press a key, and have it attached to a new instance of ConEmu. I tried to write an AutoHotKey macro to attach the currently focused console application when I press Win+A, but I can't figure out how to use ConEmuC. Here's my attempt:

#IfWinActive, ahk_class ConsoleWindowClass
#a::
WinGet, app_pid, PID
Run c:\opt\ConEmu-120704\ConEmu.exe /detached, , , conemu_pid
Run c:\opt\ConEmu-120704\ConEmu\ConEmuC.exe /ATTACH /PID=%app_pid%
Return


All I get the following error popup:

Attach to GUI was requested, but there is no console processes!

c:\opt\ConEmu-120704\ConEmu\ConEmuC.exe /ATTACH /PID=7104


Yet if I use the “Attach to” menu entry in the ConEmu instance, the console application gets attached. I need to tick the “alternative mode” checkbox introduced in version 120704, if that's relevant.

How do I use ConEmuC (or any other method) to attach an existing console application to a new ConEmu instance? Even better, how do I attach an existing instance (if I want a new instance, I can just create it beforehand)? And how do I specify alternative mode?

• Try the following

#IfWinActive, ahk_class ConsoleWindowClass
#a::
WinGet, app_pid, PID
Run c:\opt\ConEmu-120704\ConEmu\ConEmuC.exe /ATTACH /CONPID=%app_pid%
Return


Alternative mode: /ATTACH /CONPID=<pid>.

Starting ConEmu.exe /detached is unnecessary. GUI will be started automatically, if not found.

UPD. Build 120705. If you want to unconditionally start new ConEmu window, you may add /GHWND=NEW to ConEmuC arguments.

UPD 2. On the other hand, may be you do not need to do Attach at all? There is Default terminal feature which allows you to run ConEmu automatically "instead" of standard windows terminal when you run any console application.