简体   繁体   English

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

[英]Passing custom objects between C# and unmanaged C++

I have a legacy DLL which contains a couple of methods I need to call from C# code. 我有一个旧版DLL,其中包含一些我需要从C#代码调用的方法。 One of these methods takes in a pointer to another C++ class that lives within other C++ programs, not the DLL. 这些方法之一采用了指向其他C ++类(而不是DLL)中存在的另一个C ++类的指针。

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? 做到这一点的最佳方法是什么?

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 ++包装器?

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

如果只是几种方法使用Swig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM