将C ++结构编组为C#的最有效方法是什么? [英] What's the most efficient way to marshal C++ structs to C#?

查看:91
本文介绍了将C ++结构编组为C#的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开始读取大量的二进制文件,每个二进制文件具有1000条或更多的记录.新文件不断添加,因此我正在编写Windows服务来监视目录并在收到新文件时对其进行处理.这些文件是使用c ++程序创建的.我已经在c#中重新创建了结构定义,并且可以很好地读取数据,但是我担心这样做的方式最终会杀死我的应用程序.

I am about to begin reading tons of binary files, each with 1000 or more records. New files are added constantly so I'm writing a Windows service to monitor the directories and process new files as they are received. The files were created with a c++ program. I've recreated the struct definitions in c# and can read the data fine, but I'm concerned that the way I'm doing it will eventually kill my application.

using (BinaryReader br = new BinaryReader(File.Open("myfile.bin", FileMode.Open)))
{
    long pos = 0L;
    long length = br.BaseStream.Length;

    CPP_STRUCT_DEF record;
    byte[] buffer = new byte[Marshal.SizeOf(typeof(CPP_STRUCT_DEF))];
    GCHandle pin;

    while (pos < length)
    {
        buffer = br.ReadBytes(buffer.Length);
        pin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        record = (CPP_STRUCT_DEF)Marshal.PtrToStructure(pin.AddrOfPinnedObject(), typeof(CPP_STRUCT_DEF));
        pin.Free();

        pos += buffer.Length;

        /* Do stuff with my record */
    }
}

我认为我不需要使用GCHandle,因为我实际上并没有与C ++应用程序通信,所有操作都由托管代码完成,但是我不知道其他方法.

I don't think I need to use GCHandle because I'm not actually communicating with the C++ app, everything is being done from managed code, but I don't know of an alternative method.

推荐答案

对于您的特定应用程序,只有一件事会给您确定的答案:对它进行概要分析.

For your particular application, only one thing will give you the definitive answer: Profile it.

这就是我在使用大型PInvoke解决方案时吸取的教训.封送数据的最有效方法是封存可漂白的字段.这意味着CLR可以简单地执行相当于在本地代码和托管代码之间移动数据的memcpy的操作.简而言之,从结构中获取所有非内联数组和字符串.如果它们存在于本机结构中,则用IntPtr表示它们并将所需的值编组为托管代码.

That being said here are the lessons I've learned while working with large PInvoke solutions. The most effective way to marshal data is to marshal fields which are blittable. Meaning the CLR can simple do what amounts to a memcpy to move data between native and managed code. In simple terms, get all of the non-inline arrays and strings out of your structures. If they are present in the native structure, represent them with an IntPtr and marshal the values on demand into managed code.

我从未分析过使用Marshal.PtrToStructure与使用本机API取消引用该值之间的区别.如果通过性能分析将PtrToStructure视为瓶颈,则可能应该投资这一点.

I haven't ever profiled the difference between using Marshal.PtrToStructure vs. having a native API dereference the value. This is probably something you should invest in should PtrToStructure be revealed as a bottleneck via profiling.

对于大型层次结构,按需封送与一次将整个结构放入托管代码相对应.处理大型树形结构时,我遇到最多的问题.如果可以将节点编组,并且性能明智,那么编组单个节点的速度非常快,它可以仅编组当时需要的内容.

For large hierarchies marshal on demand vs. pulling an entire structure into managed code at a single time. I've run into this issue the most when dealing with large tree structures. Marshalling an individual node is very fast if it's blittable and performance wise it works out to only marshal what you need at that moment.

这篇关于将C ++结构编组为C#的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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