这是一些 C 样板,用于在 linux(可能还有其他 unix)上生成和与终端程序通信
int master, slave;
struct winsize wsize = {24, 80, 0, 0}; // 24 rows and 80 columns
if (openpty(&master, &slave, NULL, NULL, &wsize) < 0)
die("Failed to open the pty master/slave");
if (!fork()) {
// child, set session id and copy the pty slave to std{in,out,err}
setsid();
dup2(slave, STDIN_FILENO);
dup2(slave, STDOUT_FILENO);
dup2(slave, STDERR_FILENO);
close(master);
close(slave);
// then use one of the exec* variants to start executing the terminal program
}
// parent, close the pty slave
close(slave);
// At this point, we can read/write data from/to the master fd, and to the child
// process it would be the same as a user was interacting with the program
我知道 windows 没有fork()
or openpty()
,所以我的问题是:如何在 windows 上实现类似的功能?
如果可能的话,我希望看到执行以下操作所需的最少工作 C/C++ 代码:
- 使用 cmd.exe 生成交互式会话
CreateProcess
- 获取一组句柄/文件描述符,可用于从/向生成的进程读取/写入数据,以模拟交互式控制台会话的方式。