将嵌套数组从 .NET 公开给 COM

发布于 2024-07-06 08:34:13 字数 456 浏览 10 评论 0原文

我在 .NET (C#) 中有一个方法,它返回 string[][]。 当使用 RegAsm 或 TlbExp(来自 .NET 2.0 SDK)为包含程序集创建 COM 类型库时,我收到以下警告:

警告:没有对嵌套数组的封送支持。

此警告会导致相关方法不会导出到生成的类型库中。 有人告诉我有办法解决这个问题,使用 Variant 作为 COM 返回类型,然后在 COM 客户端进行强制转换等。 对于这个特定的程序集,目标客户端受众是 VB6。 但实际上如何在 .NET 端执行此操作?

注意:我有一个现有的遗留 DLL(及其导出的类型库),其返回类型为 Variant,但是这个 DLL(和 .tlb)是使用 .NET 之前的遗留工具生成的,因此我无法使用它们。

如果程序集是用 VB.NET 编写的,会有帮助吗?

I have a method in .NET (C#) which returns string[][]. When using RegAsm or TlbExp (from the .NET 2.0 SDK) to create a COM type library for the containing assembly, I get the following warning:

WARNING: There is no marshaling support for nested arrays.

This warning results in the method in question not being exported into the generated type library. I've been told there's ways around this using Variant as the COM return type, and then casting/etc on the COM client side. For this particular assembly, the target client audience is VB6. But how do you actually do this on the .NET side?

Note: I have an existing legacy DLL (with its exported type library) where the return type is Variant, but this DLL (and the .tlb) is generated using pre-.NET legacy tools, so I can't use them.

Would it help at all if the assembly was written in VB.NET instead?

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

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

发布评论

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

评论(2

走过海棠暮 2024-07-13 08:34:13

即使您要返回一个对象(映射到 COM Interop 中的变体),也不能解决您的问题。 VB 将能够“保留”它并“传递它”,但它无法用它做任何事情。

从技术上讲,VB 中没有与 string[][] 完全相同的东西。 但是,如果您的数组不是“锯齿状”(即所有子数组的长度相同),则应该能够使用二维数组作为返回类型。 COM Interop 应该能够翻译它。

string [,] myReturnValue = new string[rowCount,colCount];

无论您的方法正式返回一个对象(对于 VB 来说,它看起来像一个变体),还是一个字符串[,](在 VB 中看起来像一个字符串数组),都不太重要。 字符串数组是一个更好的返回,但不是必需的。

如果您的数组是锯齿状的,那么您将不得不想出一种不同的方法。 例如,您可以选择使返回的 2D 数组与子数组中最大的一个一样大,然后在单独的 [out] int[] 参数中传递长度信息,以便 VB 可以知道使用了哪些元素。

Even if you were to return an Object (which maps to a Variant in COM Interop), that doesn't solve your problem. VB will be able to "hold" onto it and "pass it around", but it won't be able to do anything with it.

Technically, there is no exact equivalent in VB for a string[][]. However, if your array is not "jagged" (that is, all the sub-arrays are the same length), you should be able to use a two-dimensional array as your return type. COM Interop should be able to translate that.

string [,] myReturnValue = new string[rowCount,colCount];

Whether your method formally returns an Object (which will look like a Variant to VB), or a string[,] (which will look like an Array of Strings in VB), is somewhat immaterial. The String array is a nicer return, but not a requirement.

If you array is jagged, then you are going to have to come up with a different method. For example, you could choose to make your return 2D array as big as the biggest of the sub-arrays, and then pass the length information in a separate [out] int[] parameter, so that VB can know which elements are used.

掐死时间 2024-07-13 08:34:13

C# 中的变体相当于 System.Object。 因此,您可能想尝试将结果返回到对象,并在另一侧将其作为变体重新拾取。

VB 没有 C# 所缺乏的任何功能,因此我怀疑如果 .NET 方面用 VB 编写会更好或更容易。

The equivalent of variant in C# is System.Object. So you might want to try to return the result cast to object and pick it back up on the other side as a variant.

VB doesn't have any facilities that C# lacks, so I doubt it would be better or easier if the .NET side was written in VB.

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