简体   繁体   English

从第三方DLL捕获StandardOutput

[英]Capture StandardOutput from a third party DLL

I have a DLL that I call from my program which outputs status to the Visual Studio Output Window (assuming this is standard output) and I want to capture that output at run time. 我有一个从程序中调用的DLL,它将状态输出到Visual Studio输出窗口(假设这是标准输出),并且我想在运行时捕获该输出。

All of the examples of how to do this assume I am using Process to launch an external app or console app and then capture the StandardOutput from the Process instance, which I have done more than a few times myself. 有关如何执行此操作的所有示例均假设我正在使用Process启动外部应用程序或控制台应用程序,然后从Process实例捕获StandardOutput,而我本人已经完成了多次。 But in this case my program calls the DLL directly -- I'm not starting an extra process. 但是在这种情况下,我的程序直接调用DLL-我没有开始额外的过程。

While my program is calling the DLL, I want to read what it prints to standard output to log it for debugging and possibly exit my program if the DLL is failing. 当我的程序调用DLL时,我想读取它打印到标准输出中的内容以记录它以进行调试,如果DLL失败,则可能退出我的程序。 When the DLL call returns, my program should continue normally. 当DLL调用返回时,我的程序应正常继续。

I know I need another thread to process the DLL output while my other thread is in the DLL call; 我知道当另一个线程在DLL调用中时,我需要另一个线程来处理DLL输出; I just don't know how to capture the output in the first place. 我只是不知道如何首先捕获输出。

Your program could call Console.SetOut to replace the standard output stream with your own implementation. 您的程序可以调用Console.SetOut用您自己的实现替换标准输出流。

This should allow you to trap standard output as needed. 这应该允许您根据需要捕获标准输出。 Note that you may also need to replace the standard error stream, depending on how the DLL is writing the output. 请注意,您可能还需要替换标准错误流,具体取决于DLL如何写入输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM