user982033
user982033

Reputation: 43

system.io.stream <-> native C++ FILE *

I got a DLL (C++ native) which requires a FILE * parameter since it uses operations such as fwrite. I don't want to export the generated data to a file, but to a .NET CLR System.IO.Stream (it's a web app and I want to use the HttpContext.Response.OutputStream)

I started with using stdout and trying to link it further to the outputstream but it gives some problems and I'm afraid it won't work well when it's multithreaded... (There is only one standard output).

I'm searching a few days for this problem and can't seem to find a good sollution.

Thanks,

Kristof

Upvotes: 0

Views: 655

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72509

The C FILE* API is not extensible. So there is no documented way to wrap a System.IO.Stream with a FILE*. Perhaps you can somehow hack the CRT, although I doubt that because it may use a global state.

Wrapping a FILE* with System.IO.Stream should be straightforward. Just implement the Stream interface which reads the sequence from the FILE*.

If you could pass an iostream object to the native interface your life would be easier. Perhaps you can modify the native part (or request the developer of this DLL) to use an iostream (in fact streambuf) interface instead of FILE*.

EDIT: There is a hack, you can create a separate process which runs the DLL and pass the stdin/stdout as the FILE* object to the native interface. On the other side create a pipe and read/write to it through a System.IO.Stream.

EDIT 2: It's possible to do this without creating a new process.

Upvotes: 1

Related Questions