[英]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不会与其他任何人共享控制台,其正常操作模式是:
stdin
. stdin
读取并解析一行。 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::cout
& std::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.