Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:55:39+00:00 2026-05-27T14:55:39+00:00

How can I stream data (text) from a managed assembly to a native library

  • 0

How can I stream data (text) from a managed assembly to a native library and stream data (text) back to the managed assembly?

Specifically, I want to expose a System.IO.Stream of some sort on the .NET side, and (most importantly) a FILE * on the native side.

The signature of the native method should be:

FILE * foo(FILE * bar);

The signature of a wrapper around the native p/invoke call should be:

CustomStream foo(CustomStream bar);

I do not want to use callback methods on the native side (one for getting more data and one for setting more data). I want to use a FILE * on the native side – and all of the associated methods which operate upon it such as fprintf.

I do not want any disk I/O. This needs to be an in-memory operation.

I have complete control over both the managed assembly and the native library.

The solution must work with .NET 2.0

I’m willing to create any sort of managed or unmanaged shim layer required to pull this off.

The “obvious” solution is to use STDIN and STDOUT and launch a child process – however I don’t want a separate process. Also, my attempts to redirect the STDIN and STDOUT streams of a native library which isn’t a console application on Windows have failed somewhat spectacularly (and with much head-banging).

Based on this question:
Redirect stdout+stderr on a C# Windows service I attempted to modify the approach to (at least) solve the “response” stream half of my problem – but without a FileStream (since I want something more analogous to a MemoryStream). However, FileStream is the only stream type which exposes a suitable low-level stream handle.

Otherwise, I’m pretty well stuck and am currently thinking I’ll need to dive deeper and come up with my own hand-rolled native<->managed stream implementation but don’t really know where to start.


Solution

Finally!

I’ve posted a complete sample project here:

http://pastebin.com/jcjHdnwz

This is for .NET 3.5 and uses the AnonymousPipeServerStream – but with a little bit of reflector-ing, it’s easy enough to duplicate the inner workings of the AnonymousPipeServerStream in .NET 2.0.

Thanks for your help shf301 for pointing me to the native pipe API, which got me looking into the Microsoft docs for a better understanding of what’s going on, and for pointing out I needed to use the _open_osfhandle method to get the FILE * reference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:55:40+00:00Added an answer on May 27, 2026 at 2:55 pm

    You should be able to do this using a AnonymousPipeStream in .NET 3.5 or higher. That exposes a handle, via the SafePipeHandle property that you can pass to SetStdHandle.

    For .NET 2.0 you may have to P/Invoke to the unmanaged pipe API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm exporting some mysql data to a text file. The data can be selected
Basically I want to stream data from memory into a tar/gz format (possibly multiple
How can I specify the filename when dumping data into the response stream? Right
Can I stream a live video in WPF application from my web cam? If
How can I extract the part of this stream (the one named BLABLABLA) from
How can I write bits to a stream (System.IO.Stream) or read in C#? thanks.
I would like to get contents of some stream or project I can find
I am using zlib to compress a stream of text data. The text data
I am trying to extract some parts of the Video: line from below text.
Using C++ and .net I have a stream of data I want to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.