如何实现windows终端仿真器



下面是一些用于在linux(可能还有其他unix)上生成终端程序并与之通信的C语言样板

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()openpty(),所以我的问题是:如何在windows上实现类似的东西?

如果可能的话,我希望看到工作所需的C/c++代码的最小量:

  • 使用CreateProcess生成cmd.exe的交互式会话
  • 获取一组句柄/文件描述符,这些句柄/文件描述符可用于从生成的进程读取/写入数据,其方式将模拟交互式控制台会话。

Windows控制台与Linux控制台的工作方式非常不同。在窗口中没有PTY或虚拟控制台来生成或连接。你基本上是在windows控制台工作。

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

如果你对基于文本的界面感兴趣,你可以看看windows的PDcurses。

最新更新