通过CLI传递从C#中的fstream(或等值)到C ++ [英] Passing an fstream (or equivalent) from C# to C++ through CLI

查看:273
本文介绍了通过CLI传递从C#中的fstream(或等值)到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能够通过从C#中的fstream或同等通过CLI非托管C ++ DLL

How can I pass an fstream or equivalent from C# through CLI to an unmanaged C++ DLL?

粗糙应用概要:


  • C#应用程序从数据库中读取的二进制文件

  • 非托管C ++ DLL是用来解码这个文件,并返回所含信息

  • 我可以修改任何C#代码。 CLI的包装是C ++一面,我可以修改的唯一部分。

我目前保存二进制文件到磁盘,通过它的路径,在那里开了作为fstream的CLI的包装。这是罚款用于测试目的,但不会生产显而易见的原因工作。

I'm currently saving the binary file to disk and passing the path of it to the CLI wrapper where it is opened as an fstream. This is fine for test purposes, but won't work for production for obvious reasons.

我也看着传递一个字节数组到DLL但我不能够找到一种方法将其转换成比的GlobalAlloc,我宁愿不使用其他的fstream的。

I've also looked into passing a byte array to the DLL but I was not able to find a way to convert that to an fstream other than with GlobalAlloc, which I would prefer not to use.

任何帮助或想法,将不胜感激。

Any help or ideas would be appreciated.

感谢。

推荐答案

您可以通过一个管理二进制数组到C ++ / CLI DLL。针脚阵列。这然后可以被转换为一个STL字符串对象。然后,您可以通过STL字符串转换为STL stringstream的对象,从继承的iostream。想想字符串流为.NET MemoryBuffer的对象。通过字符串流你的非托管C ++。这也许可以上述<进行; 10行代码。缺点是数据在内存中这是低效的被复制。对于许多应用程序,我怀疑这将是一个问题。

You can pass a managed binary array to the C++/CLI DLL. Pin the array. This can then be converted to an STL string object. You can then pass the STL string into an STL stringstream object, which inherits from iostream. Think of stringstream as a .NET MemoryBuffer object. Pass stringstream to your unmanaged C++. That can probably be done in < 10 lines of code. The downside is the data will get copied in memory which is inefficient. For many applications I doubt it would be an issue.

另外,您也可以编写自己的类从stream_buffer一个封装了.NET流对象继承。 (最好从这个代替的iostream继承,正如其他建议)。这将是最有效的方式,因为没有记忆会被不必要的复制,但我不会刻意做,如果第一种方法是不够快。

Alternatively, you could write your own class inheriting from stream_buffer that wraps a .NET stream object. (Better to inherit from this instead of iostream, as others propose). That would be the most efficient way because no memory would be needlessly copied but I wouldn't bother doing it if the first method is fast enough.

这篇关于通过CLI传递从C#中的fstream(或等值)到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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