2

这是一些 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
  • 获取一组句柄/文件描述符,可用于从/向生成的进程读取/写入数据,以模拟交互式控制台会话的方式。
4

1 回答 1

0

Windows 控制台的工作方式与 Linux 控制台非常不同。Windows 中没有可生成或连接的 PTY 或虚拟控制台。您基本上在 Windows 控制台本身中工作。

然后,您将处理您自己的所有 I/O 以模拟终端,跟踪控制台作为您的窗口、x/y 位置坐标、颜色等。

如果您对基于文本的界面感兴趣,可以查看类似 PDcurses for windows 的内容。

于 2015-09-30T22:48:52.130 回答