调用从VB6通过COM可见的DLL .NET方法方法、COM、NET、DLL

2023-09-02 21:40:48 作者:别在哥面前耍酷

我创建了一个.NET的DLL,这使得一些方法COM可见。

I have created a .NET DLL which makes some methods COM visible.

一种方法是有问题的。它看起来是这样的:

One method is problematic. It looks like this:

bool Foo(byte[] a, ref byte[] b, string c, ref string d)

VB6给出一个编译错误,当我试图调用方法:

VB6 gives a compile error when I attempt to call the method:

函数或接口标记为   受到限制,或者该函数使用一个   自动化类型不支持   Visual Basic中。

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.

我读到数组参数必须由引用传递,所以我改变了签名的第一个参数:

I read that array parameters must be passed by reference, so I altered the first parameter in the signature:

bool Foo(ref byte[] a, ref byte[] b, string c, ref string d)

VB6仍然给出了同样的编译错误。

VB6 still gives the same compile error.

我怎么可能改变签名是用VB6兼容?

How might I alter the signature to be compatible with VB6?

推荐答案

声明与裁判的阵列参数是必须的。你的第2次尝试应该有工作就好了,也许你忘了重新生成的.tlb?

Declaring the array argument with "ref" is required. Your 2nd attempt should have worked just fine, perhaps you forgot to regenerate the .tlb?

测试code:

[ComVisible(true)]
public interface IMyInterface {
 bool Foo(ref byte[] a, ref byte[] b,string c, ref string d);
}

[ComVisible(true)]
public class MyClass : IMyInterface {
  public bool Foo(ref byte[] a, ref byte[] b, string c, ref string d) {
    throw new NotImplementedException();
  }
}


  Dim obj As ClassLibrary10.IMyInterface
  Set obj = New ClassLibrary10.MyClass
  Dim binp() As Byte
  Dim bout() As Byte
  Dim sinp As String
  Dim sout As String
  Dim retval As Boolean
  retval = obj.Foo(binp, bout, sinp, sout)