English 中文(简体)
如何将C++本机对象协调到托管C++CLI
原标题:How to Marshall C++ Native Objects to Managed C++ CLI

我有一堆原生C++对象,类包含DTL映射、映射的映射、列表和向量。

我需要从C++本机代码中调用托管C++函数,并需要将这些本机对象和STL容器(列表、映射、映射的映射)传递给C++/CLI。它需要封送或以某种方式序列化这些对象。我怎么能在没有任何问题的情况下做到这一点。因此,在编组并序列化回托管C++/CLI之后,映射应该与字典和字典的字典一起编组,stl-list with managed list<>;等等

我怎样才能在所有情况下都做到这一点?是否需要复杂处理编组问题。。。?

Regards Usman

问题回答

STL内存布局是特定于实现的。例如,当您使用Visual C++附带的实现时,sizeof(std::vector)在发行版中为16,在调试模式中为20。并且STL类中的指针无法有意义地封送至托管内存。如果您想进行封送处理,您可以在接口中切换到与平台无关的C或COM类型(例如,传递带有计数参数的数组或安全数组),因为.Net对这些类型有更好的了解。我推荐COM,因为它有更丰富的类型,并在您需要时支持其他语言。

或者,如果你需要速度,你可以写一个marshali_as模板函数进行转换,以便您可以重用封送处理代码甚至封送处理缓冲区,或者为C++对象编写一个托管包装器

如果要封送的数据太大,无法容纳在内存中,您还可以将数据序列化到临时文件或数据库,并以块的形式从托管代码中读回它们。





相关问题
Marshal struct with array member in C#

I m using C# with P/Invoke to access to a DLL method. The definition of the method is the following: [DllImport("userManager.dll")] static extern int GetUsers(out IntPtr userList); Original structs: ...

Are double* and double** blittable types? C#

I have a question regarding marshalling of C++ arrays to C#. Does the double* automatically convert to double[]? I know double is a blittable type, so double from C++ is the same as double from C#. ...

How do I unmarshal a ruby object in java?

I have a an object that I d like to grab the contents of in java. The only problem is that is is currently in ruby. irb(main):050:0> blah => "...

Marshalling an array of structures from C++ to C#?

In my C# code I m trying to fetch an array of structures from a legacy C++ DLL (the code I cannot change). In that C++ code, the structure is defined like this: struct MyStruct { char* id; ...

Copy bytes in memory to an Array in VB.NET

unfortunately I cannot resort to C# in my current project, so I ll have to solve this without the unsafe keyword. I ve got a bitmap, and I need to access the pixels and channel values directly. I d ...

热门标签