Redirecting native dll stdout / stderr from c #

I am trying to redirect the output of a third-party third-party dll that outputs to stdout / stderr from C #. The output of both stdout and stderr should go to the log file.

Here is my idea (x2 for two threads):

  • Create AnonymousPipeServerStream
  • Get pipe descriptor through _outServer.SafePipeHandle.DangerousGetHandle()
  • Use P / Invoke to call SetStdHandle with the specified handle
  • Create an AnonymousPipeClientStream connected to the server thread.
  • Create a thread to sit in a loop read from AnonymousPipeClientStreamand output to the logger.
  • Call flushon periodicallyAnonymousPipeServerStream

So, all this seems to work well ... inside my code. As soon as control passes to the native DLL, everything returns to stderr! If necessary, I can debug in my own DLL and see what is going wrong, but ... I really would not like to see if anyone has any ideas before I spend 10 hours trying to figure out how do pens work?

For reference, test code: http://pastebin.com/f3eda7c8 . An interesting material is lines 58-89 in the constructor. (I, of course, will add error handling, etc.).

+3
source share
1 answer

, () . Dll mingw, , -, , MSVC.

, - .

+3

Source: https://habr.com/ru/post/1717778/


All Articles