简体   繁体   English

在附加的控制台中输入文本时,如何避免“未被识别为内部或外部命令,可操作程序或批处理文件”?

[英]How to avoid “is not recognized as an internal or external command, operable program or batch file” when entering text in attached console?

I attach existing console form C++ program. 我附加了现有的控制台形式的C ++程序。 Output works fine, but when I try to put some input in console, terminal originally running there try to handle it. 输出工作正常,但是当我尝试在控制台中输入一些内容时,最初在此运行的终端会尝试处理它。 Program code is here: 程序代码在这里:

#include <iostream>
#include "windows.h"

int main(int argc, char **argv)
{
    pid_t pid = atoi(argv[1]);
    BOOL fret = FreeConsole();
    if  (!fret)
    {
        std::cerr << "ERR" << GetLastError() << std::endl;
    }

    BOOL aret = AttachConsole(pid);

    if  (!aret)
    {
        std::cerr << "ERR" << GetLastError() << std::endl;
    }

    SetConsoleTitleA("TTTTTTTT");

    for (int ii = 0; ii < 10; ii++)
    {
        std::cout << "ii " << ii << std::endl;
    }

    int kk;
    std::cin >> kk;

    std::cerr << "kk " << kk << std::endl;

    return 0;
}

When I run cmd, find out PID, run my program and then write for example 12 and enter, this is result: 当我运行cmd时,找出PID,运行程序,然后编写示例12并输入,结果如下:

C:\>ii 0
ii 1
ii 2
ii 3
ii 4
ii 5
ii 6
ii 7
ii 8
ii 9
12
'12' is not recognized as an internal or external command,
operable program or batch file.

When I try once again write 12, it works better (but there is still C:>) 当我再次尝试写入12时,它会更好地工作(但是仍然有C:>)

C:\>12
kk 12
C:\>

Edit: I need redirect stdin, stdout, and stderr to console, I can run something else then cmd or send some command to cmd. 编辑:我需要将stdin,stdout和stderr重定向到控制台,然后我可以运行其他命令,然后将命令发送到cmd。

"is not recognized as an internal or external command, operable program or batch file." “未被识别为内部或外部命令,可操作程序或批处理文件。” is a standard cmd.exe error message. 是标准的cmd.exe错误消息。 cmd.exe is already waiting for input when you do AttachConsole so the first input is sent to cmd.exe and that is why you get that error message. 当您执行AttachConsole时,cmd.exe已经在等待输入,因此将第一个输入发送到cmd.exe,这就是为什么您收到该错误消息的原因。 Replace the first number input with a command like dir and you will see that this input is received by cmd.exe. 将第一个数字输入替换为dir这样的命令,您将看到cmd.exe接收了此输入。

The second time it works but mostly by accident. 第二次运行,但大多数是偶然的。 cmd.exe is probably busy with other things so your application gets the input before cmd.exe is able to "listen" to stdin again. cmd.exe可能正在忙于其他事情,因此您的应用程序会在cmd.exe能够再次“侦听” stdin之前获取输入。 cmd.exe prints the prompt (the "path" + > ) because that's what it does after each command has been performed. cmd.exe显示提示符(“ path” + > ),因为这是在执行每个命令之后执行的操作。

cmd.exe does not expect to share the console with anyone else and its normal mode of operation is to: cmd.exe不会与其他任何人共享控制台,其正常操作模式是:

  1. Read and parse a line from stdin . stdin读取并解析一行。
  2. Execute. 执行。 Wait for the operation to complete if the command is an internal command or a external console application. 如果命令是内部命令或外部控制台应用程序,请等待操作完成。 It does not wait for GUI applications. 它不等待GUI应用程序。
  3. Print %PROMPT%. 打印%PROMPT%。
  4. Goto 1. 转到1。

Even if you could convince cmd.exe to coexist with you, what you are doing is probably undefined behavior because you are mixing low-level native Windows console functions ( AttachConsole ) with high-level C++ I/O functions ( std::cout & std::cin ). 即使您可以说服cmd.exe与您共存,但您所做的可能是未定义的行为,因为您将低级本机Windows控制台函数( AttachConsole )与高级C ++ I / O函数( std::coutstd::cin )。 Assuming you are using the Microsoft CRT you could use _open_osfhandle + _dup2 to correctly assign the native console handle but you would still have issues with cmd.exe and there is no way to work around it. 假设您使用的是Microsoft CRT,则可以使用_open_osfhandle + _dup2正确分配本机控制台句柄,但cmd.exe仍然有问题, _dup2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;g++&#39; 不是内部或外部命令、可运行程序或批处理文件 - 'g++' is not recognized as an internal or external command, operable program or batch file 无法将c ++ win32控制台应用程序识别为内部或外部命令可操作程序或批处理文件 - c++ win32 console application is not recognized as an internal or external command operable program or batch file 如何在 Atom 中编译和运行程序? 我得到这个“不被识别为内部或外部命令、可运行程序或批处理文件”。 - How to compile and run a program in Atom? I get this "is not recognized as an internal or external command, operable program or batch file." &#39;mblctr&#39; 不是内部或外部命令,也不是可运行的程序或批处理文件。如何解决这个问题? - 'mblctr' is not recognized as an internal or external command, operable program or batch file.How to fix this? exe文件未被识别为C ++中的内部或外部命令,可操作程序或批处理文件 - exe file is not recognized as an internal or external command, operable program or batch file in C++ VScode 终端给出输出:&#39;g++&#39; 不是内部或外部命令,也不是可运行的程序或批处理文件 - VScode Terminal giving output: 'g++' is not recognized as an internal or external command, operable program or batch file ./a.out 结果 &#39;.&#39; 不是内部或外部命令、可运行的程序或批处理文件 - ./a.out results '.' is not recognized as an internal or external command, operable program or batch file 'g++' 不是内部或外部命令、可运行程序或批处理文件。 - 'g++' is not recognized as an internal or external command, operable program or batch file. 'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。 即使正确安装了 gcc - 'g++' is not recognized as an internal or external command, operable program or batch file. even though gcc is installed correctly Windows 10 - 'make' 未被识别为内部或外部命令、可运行程序或批处理文件 - Windows 10 - 'make' is not recognized as an internal or external command, operable program or batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM