Launching multiple consoles using pseudo-console

curious.benjamin 6 Reputation points
2021-12-10T05:06:51.807+00:00

I am building a Windows console-based application that needs to launch multiple terminals for user interaction. The intention is to achieve the same using pseudo-consoles and an inbuilt windows app like conhost.exe / powershell.exe (to be run inside the pseudo-console).

Refer the below sample code for same -

         TCHAR   szCmdline[] = L"conhost.exe";

         // Communication pipes creation and pseudo-console setup
         CreatePipe(&inPipeRead, &inPipeWrite, &saAttr, 0);
         CreatePipe(&outPipeRead, &outPipeWrite, &saAttr, 0);
         CreatePseudoConsole(COORD{ 90, 60 }, inPipeRead, outPipeWrite, 0, &hPCon);

         // Attributes setup
         siStartInfo.cb = sizeof(STARTUPINFO);
         siStartInfo.hStdError = outPipeWrite;
         siStartInfo.hStdOutput = outPipeWrite;
         siStartInfo.hStdInput = inPipeRead;
         siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
         InitializeProcThreadAttributeList(nullptr, 1, 0, (PSIZE_T)&size);
         lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, size);
         InitializeProcThreadAttributeList(lpAttributeList, 1, 0, (PSIZE_T)&size);
         UpdateProcThreadAttribute(lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, hPCon, sizeof(HPCON), nullptr, nullptr);

         // Pseudo-console launch as a child process
         CreateProcess(nullptr, szCmdline, nullptr, nullptr, TRUE, CREATE_NEW_CONSOLE, nullptr, nullptr, &siStartInfo, &piClient);

The idea is to create multiple terminals in multiple pseudo-consoles (each pseudo-console hosting one terminal). The communication with the master process (the process launching the pseudo-console processes) is possible through pipes created.

However, this way the terminals are not able to interact with User as the STDIN & STDOUT handles are tied with the pipes. Is there a way to manage User interaction as well as communication with main process via pipes?

Windows development Windows API - Win32
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.