簡體   English   中英

使用Visual Studio 2015編譯時,將控制台(或stdout)重定向到命名管道不再起作用

[英]Redirecting console (or stdout) to named pipe no longer works when compiling with Visual Studio 2015

我有一個腳本引擎,它將其stdout重定向到命名管道以進行調試。 自Visual Studio 6起,下面的代碼一直在工作,但是在使用VS2015進行編譯時似乎不再起作用。 不會引發任何錯誤,但是輸出將繼續寫入控制台,而不是命名管道。 當我在VS2012中編譯此應用程序時,它可以按預期工作。

hNamedPipe是我要將控制台文本重定向到的管道。

int hCrt = _open_osfhandle((intptr_t)hNamedPipe, _O_TEXT);
FILE *hf = _fdopen(hCrt, "w");
*stdout = *hf;
setvbuf(stdout, NULL, _IONBF, 0);

使用Visual C ++ v14平台工具集進行編譯時,如何將stdio重定向到命名管道?

多虧了James McNellis,我才得以得出一個解決方案:

這是設計使然。 FILE是不透明的數據類型; 您不能像在代碼中那樣取消對FILE的引用。

這是最終為我工作的代碼:

void main()
{
    HANDLE hNamedPipe = Create_Some_Named_Pipe();

    RedirectIO(stdout, hNamedPipe);
    printf("Hello World."); //This arrived at the listening named pipe.
}

void RedirectIO(FILE *hFrom, HANDLE hTo)
{
    int fd = _open_osfhandle((intptr_t)hTo, _O_WRONLY | _O_TEXT);
    _dup2(fd, _fileno(hFrom));
    setvbuf(hFrom, NULL, _IONBF, 0); //Disable buffering.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM