在 C# 和非托管 C++ 之间传递自定义对象

发布于 2024-07-11 19:30:41 字数 176 浏览 7 评论 0原文

我有一个旧版 DLL,其中包含一些我需要从 C# 代码调用的方法。 其中一个方法接受指向另一个 C++ 类的指针,该类存在于其他 C++ 程序中,而不是 DLL 中。

我很确定我需要使用 pinvoke 来回编组对象,但我还没有找到任何有关编组自定义对象的内容。

实现这一目标的最佳方法是什么?

I have a legacy DLL which contains a couple of methods I need to call from C# code. One of these methods takes in a pointer to another C++ class that lives within other C++ programs, not the DLL.

I'm pretty sure I need to use pinvoke to marshal the objects back and forth, but I haven't found anything on marshaling custom objects.

What would be the best way of accomplishing this?

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

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

发布评论

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

评论(3

软糖 2024-07-18 19:30:41

你确定这是一个类而不是一个结构体? 毕竟,不同的编译器可以选择在内存中以不同的方式布置类,这会破坏目标 DLL。

您是否考虑过使用托管 C++ 包装器?

Are you sure it's a class and not a struct? After all, different compilers could choose to lay the class out differently in memory which would break the target DLL.

Have you considered using a Managed C++ wrapper?

南渊 2024-07-18 19:30:41

我会考虑 C++/CLI,尽管 COM 互操作可能更容易,具体取决于 C++ 代码的形状。

I'd look at C++/CLI although COM interop may be easier depending on the shape of your C++ code.

初心未许 2024-07-18 19:30:41

如果只是使用 swig 的几种方法

if its just a few methods use swig

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