将 VB6 变体分配给对象

发布于 2024-08-15 19:27:29 字数 190 浏览 8 评论 0原文

我有一个使用 C# 编译的 Dll 的 VB6 应用程序。我已经通过 COM 成功地完成了这项工作。 但我的问题是我有一个 Variant 数组,其中包含 String 和 Double 数据类型。我需要将此数组传递给我的 C# Dll,它以对象形式接收该数组。 因此,我需要做的就是将 Variant 数组转换为 C#“可以理解”的对象数组。有人对此有任何线索吗?

I have a VB6 application that uses a C# compiled Dll. I have been successfull in making this work by means of COM.
But my problem is that I have a Variant array with String and Double data types inside it. I need to pass this array to my C# Dll, which is receiving the array as an Object.
So, all I need to do is to convert the Variant array into an Object array "understandable" by C#. Anyone has any clue on it?

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

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

发布评论

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

评论(4

紫竹語嫣☆ 2024-08-22 19:27:30

object[] System.Runtime.InteropServices.Marshal.GetObjectsForNativeVariants(IntPtr aSrcNativeVariant, int cVars)

您尝试过这种方法吗?

object[] result;
unsafe
{
  pin_ptr<object> pinObj = &obj;
  result = Marshal.GetObjectsForNativeVariants(new IntPtr(pinObj), objSize);
}

我自己没有尝试过,但似乎它会成功。

object[] System.Runtime.InteropServices.Marshal.GetObjectsForNativeVariants(IntPtr aSrcNativeVariant, int cVars)

Have you tried this method?

object[] result;
unsafe
{
  pin_ptr<object> pinObj = &obj;
  result = Marshal.GetObjectsForNativeVariants(new IntPtr(pinObj), objSize);
}

Haven't tried it myself but seems like it would do the trick.

以歌曲疗慰 2024-08-22 19:27:30

这是 C# 函数声明:

public double[][] CalcMatching( object[][] data1, object[][] data2, long dataLen1, long dataLen2, string matchingType )

这是 VB6 调用:

result = matchingCalcObj.CalcMatching(data1, data2, dataLen1, dataLen2, Matching)

其中 data1data2 是 Variant 数组。

我不认为我能在 C# 上做太多事情,就像你们所说的那样,一旦我在函数调用中遇到的错误是“无效的过程调用或参数”。 VB6方面有什么选择吗?

感谢您的所有回复。

This is the C# function declaration:

public double[][] CalcMatching( object[][] data1, object[][] data2, long dataLen1, long dataLen2, string matchingType )

This is the VB6 call:

result = matchingCalcObj.CalcMatching(data1, data2, dataLen1, dataLen2, Matching)

where data1 and data2 are arrays of Variant.

I don't think I can do much at the C#, like you guys are saying, once the error I get at the function calling is "Invalid procedure call or argument". Any option at the VB6 side?

Thanks for all the replies.

一抹微笑 2024-08-22 19:27:29

这应该可以解决问题

ArrayList a = new ArrayList(YourObjectArrayHere);

This should do the trick

ArrayList a = new ArrayList(YourObjectArrayHere);
流年里的时光 2024-08-22 19:27:29

这必须从 C# 方面直接完成;如果不是,那么 VB6 就无能为力。也就是说,默认情况下,这样声明的方法:

void Foo(object[] a);

从 VB6 中将被视为采用 Variant 数组(或者,在 IDL 级别上,为 SAFEARRAY(VARIANT)) 。

如果它不适合您,那么您的 C# 声明就有问题 - 请将它们发布出来,以便进行审查。

This has to be done right from C# side of things; if it's not, then there isn't much you can do from VB6. That said, by default, a method declared like this:

void Foo(object[] a);

will be seen from VB6 as taking an array of Variant (or, on IDL level, as SAFEARRAY(VARIANT)).

If it doesn't work that way for you, then there's something wrong with your C# declarations - please post them so they may be reviewed.

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