[英]How to Integrate Command prompt/Terminal in a Windows application?
Many programs like VS Code have a built-in terminal window that allows user to execute commands just as if they were working in command prompt or powershell. 像VS Code这样的许多程序都有一个内置的终端窗口,该窗口允许用户执行命令,就像它们在命令提示符或Powershell中一样。 I'm working on adding similar functionality to my winforms application, and allow user to create multiple terminal windows. 我正在为Winforms应用程序添加类似的功能,并允许用户创建多个终端窗口。
I tried to do this by launching cmd.exe as a hidden process and redirecting stdin/stdout. 我试图通过将cmd.exe作为隐藏进程启动并重定向stdin / stdout来做到这一点。 But running into issues with synchronizing stdout & stderr output. 但是遇到同步标准输出和标准错误输出的问题。 There is no sure way of knowing where to insert stderr output in the stdout output and I often end up with out-of-place error messages - sometimes in-between stdout lines. 无法确定在stdout输出中插入stderr输出的位置的方法,我常常会收到错误消息,有时是在stdout行之间。 If I use same handle for stderr/stdout, then output is synchronized correclty, however, I can't tell whether its error text so can't color code it. 如果我对stderr / stdout使用相同的句柄,则输出是同步正确的,但是,我无法确定其错误文本是否可以对其进行颜色编码。
Is my fundamental approach of running cmd.exe as a hidden process correct? 我将cmd.exe作为隐藏进程运行的基本方法正确吗? If yes, then how do I merge output from stdout/stderr? 如果是,那么如何合并stdout / stderr的输出?
If I don't misunderstand, you may be able to do like below. 如果我没有误会,您可能可以执行以下操作。
C++: C ++:
#include <windows.h>
#include <iostream>
void main()
{
char cmd[] = "C:\\Windows\\System32\\cmd.exe";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CreateProcessA(cmd, NULL, NULL, NULL, FLASE, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
}
with no dwCreationFlags
flag, The child process cmd.exe
and the parent process share the same console. 如果没有dwCreationFlags
标志,则子进程cmd.exe
和父进程共享同一控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.