2014-07-25 34 views
1

下面是產卵和Linux上的終端程序(可能還有其他的Unix)通信的位C樣板如何實現Windows的終端模擬器?

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 

我明白,窗戶沒有fork()openpty(),所以我的問題是:如何實現在Windows上類似的東西?

如果可能的話,我想看看做以下所需的工作C/C++代碼的最低金額:使用CreateProcess

  • 獲得一組

    • 菌種的cmd.exe的交互式會話處理/文件描述符,可以用來從模擬交互式控制檯會話的方式讀取/寫入衍生進程的數據。
  • +0

    我還沒有找到。我不明白,這個問題是如何與線程相關的? –

    +0

    'fork'的功能是什麼?你可以使用線程而不是進程? –

    +0

    不,叉有創建一個新的過程,然後運行終端程序 –

    回答

    0

    Windows控制檯的工作原理與Linux控制檯非常不同。在窗口中沒有PTY或虛擬控制檯來產生或連接到。你基本上在Windows控制檯本身工作。

    你可以這樣處理所有的I/O你的自我來模擬一個終端,跟蹤控制檯作爲你的窗口,X/Y位置座標,顏色等

    你可以看看像PDcurses窗戶如果你對基於文本的界面感興趣。