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

Rik*_*ley 2 .net arrays vb6 com assemblies

我创建了一个.NET DLL,它使一些方法COM可见.

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

bool Foo(byte[] a, ref byte[] b, string c, ref string d)
Run Code Online (Sandbox Code Playgroud)

当我尝试调用该方法时,VB6给出了编译错误:

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

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

bool Foo(ref byte[] a, ref byte[] b, string c, ref string d)
Run Code Online (Sandbox Code Playgroud)

VB6仍然提供相同的编译错误.

如何更改签名以与VB6兼容?

Han*_*ant 5

用"ref"声明数组参数是必需的.你的第二次尝试应该工作得很好,也许你忘了重新生成.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)
Run Code Online (Sandbox Code Playgroud)