开发者

c# pass file pointer to unmanaged c++ dll to use for stdout

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

From a c# console app, I'm calling some methods from an unmanaged C++ dll. The DLL writes to the stdout stream, although this was not being picked up by the c# console.

I found the following code, which 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_osfha开发者_开发知识库ndle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
}

AOK so far:

What I'd like to do is capture the stdoutfrom the DLL to a c# stream, rather than send it to the console. I tried the method detailed here (Redirect stdout+stderr on a C# Windows service), which does capture the output, however the app "crashes" when the program closes ("vshost.exe has stopped working").

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

So I thought what if I use the "Filestream.SafeFileHandle.DangerousGetHandle()" method to get a handle to the filestream from c#, and pass this into the C++ method redirect_stdout() 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 piped to the c# Console, however, the filestream on the c# side is always empty.

Can any expert give guidance to have the STDOUT write its output to the c# filestream? I'm sure I've made some stupid error about how to achieve this or I am not understanding how to achieve what I am trying to do.

Thank you for your time and input - really appreciated!

[EDIT]

OK - I've played a bit more and modified 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 );
}

This also successfully populates the c# stream, however when the c# Console app closes, the app crashes with the error "vshost.exe has stopped working". This is the same error as when I use the method from Redirect stdout+stderr on a C# Windows service

Very odd find: If I run the console app "outside of visual studio" (eg, double click on the .exe in the bin folder), there is no crash!

So I guess my next question is: how do I track down the source of this crash? Is it VS related? It occurs in either debug or release mode when running from VS, and no crash when run outside of VS.

I am at a loss as to how to debug this one!


You might consider using named pipes: i.e

communication between c++ and c# through pipe

http://www.switchonthecode.com/tutorials/interprocess-communication-using-named-pipes-in-csharp

Hopefully, that should work...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜