简体   繁体   English

如何在Windows应用程序中集成命令提示符/终端?

[英]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.

相关问题 在Windows应用程序中执行的命令提示符命令 - command prompt commands executing in windows application 如何检测正在执行应用程序的上下文? 无论是从命令提示符还是从Windows窗体内部 - How to detect the context from which an application is being executed? Whether from Command Prompt or from within a Windows Form 如何在C#for .NET中从Windows窗体应用程序发送命令提示符参数? - How Do I Send Command Prompt Arguments from a Windows Form Application in C# for .NET? 打开命令提示符并从Windows窗体应用程序执行命令 - open Command Prompt and execute commands from Windows Form Application 在Windows命令提示中使用C#应用程序打开记事本 - Open Notepad with C# application in Windows Command Prompt 从控制台应用程序获取响应(Windows中的命令提示符) - Get Response from Console Application (command prompt in windows) 如何在Windows窗体应用程序中集成opengl窗口 - how to integrate the opengl window in windows form application 如何在我自己的控制台应用程序中执行命令提示符命令 - how to execute a command prompt command in my own console application 如何将GUI dll集成到另一个应用程序(例如Windows或wpf应用程序) - how to integrate a GUI dll into another application (like windows or wpf application) 如何创建也可以在命令提示符下运行的Windows窗体程序 - How to create a Windows Forms program that can also run at the command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM