C # pass file pointer to unmanaged dll dll for use in stdout

Please bear with me - I am a C # developer with little experience with C ++, and this is a steep learning curve!

From a C # console application, I call some methods from an unmanaged dll dll. The DLL writes to the stdout stream, although this was not received by the C # console.

I found the following code that I added to the C ++ dll, which now successfully sends the contents of "printf" to the C # console.

#include <windows.h> #include <stdio.h> #include <fcntl.h> #include <io.h> void redirect_stdout() { int hConHandle; long lStdHandle; FILE *fp; // allocate a console for this app AllocConsole(); // redirect unbuffered STDOUT to the console lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); } 

AOK:

What I would like to do is grab stdoutfrom from a DLL into a C # stream, and not send it to the console. I tried the method described here ( Redirect stdout + stderr to a Windows C # service ), which captures the output, however the application crashes when the program closes ("vshost.exe stops working").

(Note: setting Console.SetOut () in the stream captures C # output, not C ++ output).

So, I thought that if I use the " Filestream.SafeFileHandle.DangerousGetHandle() " method to get the stream handle from C # and pass it to the redirect_stdout () method of the C ++ method:

 void redirect_stdout(FILE *passedInHandle) { // allocate a console for this app AllocConsole(); *stdout= *passedInHandle; setvbuf( stdout, NULL, _IONBF, 0 ); } 

When I run the above version, the output from the DLL is no longer passed to the C # console, however the stream on the C # side is always empty.

Can any expert give directions for STDOUT to write its output to a C # stream? I'm sure I made some stupid mistake about how to achieve this, or I don’t understand how to achieve what I'm trying to do.

Thank you for your time and input - very grateful!

[EDIT]

OK - I played a little and changed the C ++ method as such:

 void redirect_stdout(int passedInHandle) { int hConHandle; long lStdHandle; FILE *fp; // allocate a console for this app AllocConsole(); hConHandle = _open_osfhandle(passedInHandle, _O_TEXT); fp = _fdopen(hConHandle, "w"); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); } 

It also successfully populates the C # stream, however, when the C # Console application closes, the application crashes with the error "vshost.exe stops working." This is the same error as when using the Redirect method stdout + stderr in a C # Windows service

A very strange find: If I run the console application “outside the visual studio” (for example, double-click the .exe file in the bin folder), there is no crash!

So, I think, my next question is: how can I find the source of this failure? Is this related to VS? This happens in debug or release mode when starting from VS and without failures when working outside of VS.

I don’t understand how to debug this one!

+4
source share

All Articles