我如何可以流从托管程序集数据到本地库,然后再返回? [英] How can I stream data from a managed assembly to a native library and back again?

查看:283
本文介绍了我如何可以流从托管程序集数据到本地库,然后再返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以流从托管程序集数据(文本),以本机库和流数据(文本),回托管程序集?

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

具体而言,我要揭露一个的System.IO.Stream 在.NET侧某种和(最重要的)一 FILE * 关于本机端。

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 *富(FILE *巴);

各地土特产P A封装的签名/ Invoke调用应该是:

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

CustomStream富(CustomStream条);

我不希望使用本机端回调方法(一个用于获取更多的数据,一个用于设置更多的数据)。我想用一个 FILE * 关于本机端 - 和所有在它该操作,相关的方法 fprintf中

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 / O。这需要在内存中运行。

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.

该解决方案必须使用.NET 2.0

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.

在明显的解决方案是使用标准输入 STDOUT 并启动一个子进程 - 但是我不'吨要一个单独的进程。此外,我试图重定向的机库是不能在Windows控制台应用程序的标准输入 STDOUT 流失败了几分壮观(和多撞头)。

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).

根据这样一个问题: <一href="http://stackoverflow.com/questions/1579074/redirect-stdoutstderr-on-a-c-sharp-windows-service">Redirect标准输出+标准错误上一个C#Windows服务我试图修改的方法(至少)解决我的问题的反应流一半 - 但没有一个的FileStream (因为我想要的东西更类似于一个的MemoryStream )。然而,的FileStream 是公开了一个合适的低级别的流处理的唯一流类型。

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.

否则,我是pretty的好卡,我目前想我需要潜得更深,并拿出自己的手卷原生&LT; - >管理流实现,但真的不知道从哪里开始。

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.

终于来了!

我已经发布了一个完整的示例项目位置:

I've posted a complete sample project here:

http://pastebin.com/jcjHdnwz

这是.NET 3.5,并使用 AnonymousPipeServerStream - 但与反射器ING一点点,这是很容易复制的<$ C的内部运作$ C> AnonymousPipeServerStream 在.NET 2.0。

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.

感谢您的帮助 shf301 指着我的原生管API,让我寻找到微软文档来更好地理解这是怎么回事,并指出我需要使用 _open_osfhandle 方法来获取 FILE * 引用。

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.

推荐答案

您应该可以使用,以做到这一点<一个href="http://msdn.microsoft.com/en-us/library/system.io.pipes.anonymouspipeserverstream.aspx">AnonymousPipeStream在.NET 3.5或更高版本。这暴露了一个手柄,通过 SafePipeHandle 属性,可以传递给 SetStdHandle

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.

有关.NET 2.0中,您可能需要的P / Invoke非托管管道的API。

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

这篇关于我如何可以流从托管程序集数据到本地库,然后再返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆