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

问题描述:

我创建了一个 .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?

需要使用ref"声明数组参数.您的第二次尝试应该可以正常工作,也许您忘记重新生成 .tlb?

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

测试代码:

[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)