如何编组 C++本机对象到托管 C++命令行界面

发布于 2024-09-28 16:16:34 字数 295 浏览 7 评论 0原文

我有一堆本机 C++ 对象和类,其中包含 DTL 映射、映射的映射以及列表和向量。

我需要从 C++ 本机代码调用托管 C++ 函数,并且需要将这些本机对象和 STL 容器(列表、映射、映射的映射)传递给 C++/CLI。它需要编组或序列化这些对象。我怎样才能毫无问题地做到这一点。因此,在编组和序列化回托管 C++/CLI 后,映射应使用字典和字典的字典进行编组,stl list 与托管 List<> 进行编组。等等..

我怎样才能在所有情况下实现这一目标?是否需要复杂的编组问题处理......?

问候 乌斯曼

I have bunch of native C++ objects and classes contains DTL maps, maps of maps and lists and vectors.

I need to call managed C++ functions from C++ native code and need to pass these native objects and STL containers(lists,maps , maps of maps) to C++/CLI. It needs to marshal or some how serialize these objects. How can I do that with out any problem. So that After marshalling and serializing back to managed C++/CLI , maps should marshalled with dictionaries and dictionaries of dictionaries, stl list with managed List<> and so on..

how can I achieve this for all cases? Is it requires complex handling of marshalling issues...?

Regards
Usman

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-10-05 16:16:34

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

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

如果被封送的数据太大而无法装入内存,您还可以将数据序列化到临时文件或数据库,然后从托管代码中分块读回它们。

STL memory layout is implementation specific. E.g. sizeof(std::vector) is 16 in release and 20 in debug mode when you use the implementation comes with Visual C++. And you have pointers in STL classes that can't be marshaled meaningfully to managed memory. You can switch to platform-independent C or COM types in the interface (e.g. pass an array with a count parameter or a safe array) if you want to do marshaling as .Net has better understanding on these types. I recommend COM because it has richer types and support other languages in case you need it.

Alternatively if you need speed you can write a marshal_as template function to do the conversion so you can reuse the marshaling code or even the marshaling buffer, or write a managed wrapper for your C++ objects.

If the data being marshaled is too large to fit in the memory you can also serialize the data to temp file or database and read them back from managed code in chunks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文