将对象列表从c#传递给穷人c ++ win32 native dll的最有效方法是什么?

时间:2022-09-02 00:27:15

I need to create a list (array) from .net which consists of about 50 000 elements, pass it to c++ dll, operate on it and return a list (array) from c++ to .net.

我需要从.net创建一个列表(数组),它包含大约50 000个元素,将它传递给c ++ dll,对它进行操作并从c ++返回一个列表(数组)到.net。

  1. First option which comes to my mind, is to create a struct on both sides. Return an array of structs from .net to c++.
  2. 我想到的第一个选择是在两侧创建一个结构。将.net中的结构数组返回给c ++。

Here are my concerns:

以下是我的担忧:

a) if in a struct, it consists non reference types like: ints, doubles, etc will an array of structs with its values will be stored on stack?

a)如果在一个结构中,它包含非引用类型,如:ints,double,等等,结构数组及其值将存储在堆栈中吗?

Is there a limit when creating array of struct? Is it efficient?

创建struct数组时是否有限制?它有效吗?

Is it an efficient way to initialize such a large array on .net side?

这是在.net端初始化这么大的数组的有效方法吗?

  1. Do you have any sample which shows how to pass references to objects, etc? Without COM, interoperability, etc?
  2. 你有任何展示如何传递对象引用的样本吗?没有COM,互操作性等?

Generally I seek an advise how to perform efficiently the following things:

一般来说,我寻求建议如何有效地执行以下事项:

1) Fetch data from db 2) Allocate it in a structure which I could efficiently pass to a dll c++ win32 library 3) Perform operations on c++ side, then return an array back to .net

1)从db获取数据2)将其分配到一个我可以有效传递给dll c ++ win32库的结构中3)在c ++端执行操作,然后将数组返回给.net

I also need an advise, on which side the objects should be allocated/deallocated in terms of performing the above operations..

我还需要一个建议,在执行上述操作方面,应该在哪一方面分配/取消分配对象。

Thanks for help in advance

提前感谢您的帮助

P.S. I also don't understand the info that making an array public in a class, makes a whole copy every time I access it ... Could someone explain that to me?

附:我也不理解在一个类中公开数组的信息,每次访问它时都会制作一个完整的副本...有人可以向我解释一下吗?

1 个解决方案

#1


I think the way to do this is the simplest, and that is to pass the buffer to the C++ DLL, along with a length argument denoting the number of items. This way, the client is responsible for creating the buffer and not the DLL. The DLL is no longer responsible for creation or deletion of the buffer, just the manipulation of the buffer.

我认为这样做的方法最简单,就是将缓冲区传递给C ++ DLL,以及表示项目数的长度参数。这样,客户端负责创建缓冲区而不是DLL。 DLL不再负责创建或删除缓冲区,只是缓冲区的操作。

This is the way that most, if not all Windows API functions accomplish this. Most of those API functions deal with character buffers, but the same principle applies.

这是大多数(如果不是所有)Windows API函数实现此目的的方式。这些API函数中的大多数都处理字符缓冲区,但同样的原则也适用。

For structs, here is a link describing how to define your C# struct to be compatible with a C++ (or C) struct:

对于结构体,这里有一个链接,描述如何定义C#结构以与C ++(或C)结构兼容:

Convert C++ struct to C#

将C ++结构转换为C#

#1


I think the way to do this is the simplest, and that is to pass the buffer to the C++ DLL, along with a length argument denoting the number of items. This way, the client is responsible for creating the buffer and not the DLL. The DLL is no longer responsible for creation or deletion of the buffer, just the manipulation of the buffer.

我认为这样做的方法最简单,就是将缓冲区传递给C ++ DLL,以及表示项目数的长度参数。这样,客户端负责创建缓冲区而不是DLL。 DLL不再负责创建或删除缓冲区,只是缓冲区的操作。

This is the way that most, if not all Windows API functions accomplish this. Most of those API functions deal with character buffers, but the same principle applies.

这是大多数(如果不是所有)Windows API函数实现此目的的方式。这些API函数中的大多数都处理字符缓冲区,但同样的原则也适用。

For structs, here is a link describing how to define your C# struct to be compatible with a C++ (or C) struct:

对于结构体,这里有一个链接,描述如何定义C#结构以与C ++(或C)结构兼容:

Convert C++ struct to C#

将C ++结构转换为C#