从C#重定向本地的dll标准输出/标准错误 [英] Redirecting native dll stdout/stderr from within C#

查看:666
本文介绍了从C#重定向本地的dll标准输出/标准错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重定向一个第三方本地的dll,它输出到标准输出/标准错误从C#的输出。这两个输出和错误的输出应该去到一个日志文件。

I'm trying to redirect the output of a third-party native dll which outputs to stdout/stderr from within C#. The output of both stdout and stderr should go to a log file.

下面是我的想法(X2的两个流):

Here's my idea (x2 for two streams):

  • 创建一个 AnonymousPipeServerStream
  • 通过 _outServer.SafePipeHandle.DangerousGetHandle获取管道的手柄()
  • 使用P / Invoke来调用 SetStdHandle 与所述手柄
  • 创建一个 AnonymousPipeClientStream 连接服务器流
  • 创建一个线程坐在从 AnonymousPipeClientStream 并输出到记录仪循环阅读。
  • 定期调用刷新 AnonymousPipeServerStream
  • Create an AnonymousPipeServerStream
  • Get the pipe's handle via _outServer.SafePipeHandle.DangerousGetHandle()
  • Use P/Invoke to call SetStdHandle with said handle
  • Create an AnonymousPipeClientStream connected to the server stream
  • Create a thread to sit in a loop reading from the AnonymousPipeClientStream and outputting to the logger.
  • Periodically call flush on the AnonymousPipeServerStream

所以,这一切似乎进行得很好......在我的code。只要控制将传递到本机DLL,一切都恢复到标准错误!如果需要的话,我可以调试到本机DLL,看看发生了什么事情错了,但是......我真的不想,所以没有任何人有任何想法之前,我花10小时试图找出如何处理工作?

So all this seems to be working well... within my code. As soon as control passes to the native DLL, everything goes back to stderr! If need be, I can debug into the native DLL and see what's going wrong, but... I'd really rather not, so does anyone have any ideas before I spend 10 hours trying to figure out how handles work?

有关基准,测试code是: http://pastebin.com/f3eda7c8 。有趣的东西是在构造线58-89。 (我会在以后添加,当然错误处理,等等)。

For reference, the test code is at: http://pastebin.com/f3eda7c8. The interesting stuff is lines 58-89 in the constructor. (I'll add error handling, etc. later, of course).

推荐答案

我解决了,但解决方案(当然)已经很少做的问题。该DLL编译MinGW的,这显然不尊重所使用的MSVC运行时的手柄。

I solved it, though the solution (of course) has very little to do with the problem. The dll was compiled in mingw, which apparently doesn't respect the handles used by the MSVC runtime.

我会离开code和;解决方案在情况下,任何人都遇到了这个问题。

I'll leave the code & solution up in case anyone else encounters this problem.

这篇关于从C#重定向本地的dll标准输出/标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆