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

时间:2022-11-17 07:30:07

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.

我有一个遗留DLL,其中包含我需要从C#代码调用的几种方法。其中一个方法接受指向另一个C ++类的指针,该类存在于其他C ++程序中,而不是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.

我很确定我需要使用pinvoke来来回来编组对象,但是我没有在编组自定义对象上找到任何东西。

What would be the best way of accomplishing this?

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

3 个解决方案

#1


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.

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

Have you considered using a Managed C++ wrapper?

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

#2


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

我会看看C ++ / CLI,尽管根据C ++代码的形状,COM互操作可能会更容易。

#3


if its just a few methods use swig

如果它只是几个方法使用swig

#1


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.

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

Have you considered using a Managed C++ wrapper?

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

#2


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

我会看看C ++ / CLI,尽管根据C ++代码的形状,COM互操作可能会更容易。

#3


if its just a few methods use swig

如果它只是几个方法使用swig