<bdo id='tOFL5'></bdo><ul id='tOFL5'></ul>
    <legend id='tOFL5'><style id='tOFL5'><dir id='tOFL5'><q id='tOFL5'></q></dir></style></legend>

    <small id='tOFL5'></small><noframes id='tOFL5'>

  1. <i id='tOFL5'><tr id='tOFL5'><dt id='tOFL5'><q id='tOFL5'><span id='tOFL5'><b id='tOFL5'><form id='tOFL5'><ins id='tOFL5'></ins><ul id='tOFL5'></ul><sub id='tOFL5'></sub></form><legend id='tOFL5'></legend><bdo id='tOFL5'><pre id='tOFL5'><center id='tOFL5'></center></pre></bdo></b><th id='tOFL5'></th></span></q></dt></tr></i><div id='tOFL5'><tfoot id='tOFL5'></tfoot><dl id='tOFL5'><fieldset id='tOFL5'></fieldset></dl></div>

  2. <tfoot id='tOFL5'></tfoot>

      Windows C++:如何重定向 stderr 以调用 fprintf?

      时间:2023-09-19
    1. <legend id='FbyzO'><style id='FbyzO'><dir id='FbyzO'><q id='FbyzO'></q></dir></style></legend>
          <tbody id='FbyzO'></tbody>
        • <bdo id='FbyzO'></bdo><ul id='FbyzO'></ul>

          <small id='FbyzO'></small><noframes id='FbyzO'>

            <i id='FbyzO'><tr id='FbyzO'><dt id='FbyzO'><q id='FbyzO'><span id='FbyzO'><b id='FbyzO'><form id='FbyzO'><ins id='FbyzO'></ins><ul id='FbyzO'></ul><sub id='FbyzO'></sub></form><legend id='FbyzO'></legend><bdo id='FbyzO'><pre id='FbyzO'><center id='FbyzO'></center></pre></bdo></b><th id='FbyzO'></th></span></q></dt></tr></i><div id='FbyzO'><tfoot id='FbyzO'></tfoot><dl id='FbyzO'><fieldset id='FbyzO'></fieldset></dl></div>

            <tfoot id='FbyzO'></tfoot>
                本文介绍了Windows C++:如何重定向 stderr 以调用 fprintf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我将来自 BSD 项目的现有 C++ 代码包装在我们自己的自定义包装器中,并且我希望尽可能少地将其集成到我们的代码中.此代码使用 fprintf 打印到 stderr 以记录/报告错误.

                I am wrapping existing C++ code from a BSD project in our own custom wrapper and I want to integrate it to our code with as few changes as possible. This code uses fprintf to print to stderr in order to log / report errors.

                我想将此重定向到同一进程中的其他位置.在 Unix 上,我使用 socketpairthread 完成了此操作:套接字的一端是我发送 stderr(通过调用 dup2),另一端在一个线程中被监控,然后我可以处理输出.

                I want to redirect this to an alternative place within the same process. On Unix I have done this with a socketpair and a thread: one end of the socket is where I send stderr (via a call to dup2) and the other end is monitored in a thread, where I can then process the output.

                这在 Windows 上不起作用,因为套接字与文件句柄不同.

                This does not work on Windows though because a socket is not the same as a file handle.

                我在网上找到的所有文档都展示了如何重定向子进程的输出,这不是我想要的.如何在写入输出时在同一进程中重定向 stderr 以获得某种回调?(在你这么说之前,我已经尝试过 SetStdHandle,但找不到任何方法来完成这项工作)...

                All documents I have found on the web show how to redirect output from a child process, which is not what I want. How can I redirect stderr within the same process getting a callback of some sort when output is written? (and before you say so, I've tried SetStdHandle but cannot find any way to make this work)...

                推荐答案

                你可以在 Windows 上使用类似的技术,你只需要为相同的概念使用不同的词.:) 这篇文章:http://msdn.microsoft.com/en-us/library/ms682499.aspx 使用 win32 管道来处理来自另一个进程的 I/O,您只需要对同一进程中的线程执行相同的操作.当然,在您的情况下,从流程中任何地方到 stderr 的所有输出都将重定向到您的使用者.

                You can use a similar technique on Windows, you just need to use different words for the same concepts. :) This article: http://msdn.microsoft.com/en-us/library/ms682499.aspx uses a win32 pipe to handle I/O from another process, you just have to do the same thing with threads within the same process. Of course, in your case all output to stderr from anywhere in the process will be redirected to your consumer.

                实际上,您可能需要的其他拼图是_fdopen 和 _open_osfhandle.事实上,这是我多年前发布的一些代码的相关示例:

                Actually, other pieces of the puzzle you may need are _fdopen and _open_osfhandle. In fact, here's a related example from some code I released years ago:

                DWORD CALLBACK DoDebugThread(void *)
                {
                    AllocConsole();
                    SetConsoleTitle("Copilot Debugger");
                    // The following is a really disgusting hack to make stdin and stdout attach
                    // to the newly created console using the MSVC++ libraries. I hope other
                    // operating systems don't need this kind of kludge.. :)
                    stdout->_file = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
                    stdin->_file  = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
                    debug();
                    stdout->_file = -1;
                    stdin->_file  = -1;
                    FreeConsole();
                    CPU_run();
                    return 0;
                }   
                

                在这种情况下,主进程是一个 GUI 进程,它根本不以 stdio 句柄开始.它打开一个控制台,然后将正确的句柄推入 stdout 和 stdin,以便 debug() 函数(它被设计为 stdio 交互函数)可以与新创建的控制台交互.您应该能够打开一些管道并执行相同的操作来重定向 stderr.

                In this case, the main process was a GUI process which doesn't start with stdio handles at all. It opens a console, then shoves the right handles into stdout and stdin so the debug() function (which was designed as a stdio interactive function) can interact with the newly created console. You should be able to open some pipes and do the same sort of thing to redirect stderr.

                这篇关于Windows C++:如何重定向 stderr 以调用 fprintf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 C++ 代码中捕获 python 标准输出 下一篇:Teamviewers Quickconnect 按钮是如何实现的?

                相关文章

                最新文章

              • <i id='Pasa9'><tr id='Pasa9'><dt id='Pasa9'><q id='Pasa9'><span id='Pasa9'><b id='Pasa9'><form id='Pasa9'><ins id='Pasa9'></ins><ul id='Pasa9'></ul><sub id='Pasa9'></sub></form><legend id='Pasa9'></legend><bdo id='Pasa9'><pre id='Pasa9'><center id='Pasa9'></center></pre></bdo></b><th id='Pasa9'></th></span></q></dt></tr></i><div id='Pasa9'><tfoot id='Pasa9'></tfoot><dl id='Pasa9'><fieldset id='Pasa9'></fieldset></dl></div>
                <tfoot id='Pasa9'></tfoot>

                <legend id='Pasa9'><style id='Pasa9'><dir id='Pasa9'><q id='Pasa9'></q></dir></style></legend>
                  <bdo id='Pasa9'></bdo><ul id='Pasa9'></ul>

                  <small id='Pasa9'></small><noframes id='Pasa9'>