Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I stream data from a managed assembly to a native library and back again?
    primarykey
    data
    text
    <p>How can I stream data (text) from a managed assembly to a native library and stream data (text) back to the managed assembly?</p> <p>Specifically, I want to expose a <code>System.IO.Stream</code> of some sort on the .NET side, and (<strong>most importantly</strong>) a <code>FILE *</code> on the native side.</p> <p>The signature of the native method should be:</p> <p><code>FILE * foo(FILE * bar);</code></p> <p>The signature of a wrapper around the native p/invoke call should be:</p> <p><code>CustomStream foo(CustomStream bar);</code></p> <p><strong>I do not want to use callback methods on the native side</strong> (one for getting more data and one for setting more data). I want to use a <code>FILE *</code> on the native side - <strong>and all of the associated methods which operate upon it</strong> such as <code>fprintf</code>. </p> <p>I do not want any disk I/O. This needs to be an in-memory operation.</p> <p>I have complete control over both the managed assembly and the native library. </p> <p>The solution must work with .NET 2.0</p> <p>I'm willing to create any sort of managed or unmanaged shim layer required to pull this off.</p> <p>The "obvious" solution is to use <code>STDIN</code> and <code>STDOUT</code> and launch a child process - however I don't want a separate process. Also, my attempts to redirect the <code>STDIN</code> and <code>STDOUT</code> streams of a native library which isn't a console application on Windows have failed somewhat spectacularly (and with much head-banging).</p> <p>Based on this question: <a href="https://stackoverflow.com/questions/1579074/redirect-stdoutstderr-on-a-c-sharp-windows-service">Redirect stdout+stderr on a C# Windows service</a> I attempted to modify the approach to (at least) solve the "response" stream half of my problem - but without a <code>FileStream</code> (since I want something more analogous to a <code>MemoryStream</code>). However, <code>FileStream</code> is the only stream type which exposes a suitable low-level stream handle.</p> <p>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&lt;->managed stream implementation but don't really know where to start. </p> <hr> <h2>Solution</h2> <p>Finally! </p> <p>I've posted a complete sample project here:</p> <p><a href="http://pastebin.com/jcjHdnwz" rel="nofollow noreferrer">http://pastebin.com/jcjHdnwz</a></p> <p>This is for .NET 3.5 and uses the <code>AnonymousPipeServerStream</code> - but with a little bit of reflector-ing, it's easy enough to duplicate the inner workings of the <code>AnonymousPipeServerStream</code> in .NET 2.0.</p> <p>Thanks for your help <strong>shf301</strong> 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 <code>_open_osfhandle</code> method to get the <code>FILE *</code> reference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload