简体   繁体   English

通过 P/Invoke 将 MFC CArray 编组到 C#

[英]Marshalling an MFC CArray to C# through P/Invoke

I am attempting to write a C# class (2010) that allows the usage of functions contained in a legacy C++ dll.我正在尝试编写一个 C# class (2010),它允许使用包含在旧版 C++ Z0641Z316233FE5EC2484C 中的函数The functions are exported using __stdcall, and have varying sets of parameters.这些函数使用 __stdcall 导出,并具有不同的参数集。 I have no issue dealing with strings and other primatives, and am able to invoke these functions without issue.我处理字符串和其他原语没有问题,并且能够毫无问题地调用这些函数。

The problem is that a few of the functions contain a CArray & reference as a parameter.问题是一些函数包含一个 CArray & 引用作为参数。 I cannot figure out how to marshal this datatype, or if it's even possible.我无法弄清楚如何编组这种数据类型,或者是否有可能。

I can't change the legacy DLL code, unfortunately.不幸的是,我无法更改旧版 DLL 代码。

To deal with this correctly, you can make a wrapper DLL (using the same MFC/C++ runtime version as what you want to call into, and make sure accepts the same array in the form of, say, a traditional pointer to a native C++ array.为了正确处理这个问题,您可以制作一个包装器 DLL (使用与您要调用的相同的 MFC/C++ 运行时版本,并确保以传统指针的形式接受相同的数组,例如指向本机 C++大批。

This wrapper DLL can be called into from C#.可以从 C# 调用此包装器 DLL。

You could try with a more recent MFC version (and use C++/CLI for the marshalling), but then the new CArray could be binary incompatible with what to legacy library expects.您可以尝试使用更新的 MFC 版本(并使用 C++/CLI 进行编组),但是新的CArray可能与遗留库所期望的二进制不兼容。

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

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