如何传递VB6字符串数组[假设,s =数组(" a"," b"," c"," d&# 34;)]到C#.Net通过COM Interop?
我尝试将C#字符串数组传递给VB,将VB字符串数组传递给C#,如下所示C# - > VB正常工作但其他方式(VB => C#)给出了一个名为"的编译错误。标记为受限制的功能或界面,或者该功能使用visual basic" 中不支持的自动化类型。我的代码在下面
C#
public interface ITest
{
string[] GetArray();
void SetArray(string[] arrayVal );
}
public class Test : ITest
{
string[] ITest.GetArray() { //Working fine
string[] stringArray = { "red ", "yellow", "blue" };
return stringArray;
}
}
void ITest.SetArray(string[] arrayVal) //Giving an issue
{
string[] stringArray1 = arrayVal;
}
VB
Dim str As Variant
Debug.Print ".NET server returned: "
For Each str In dotNETServer.GetArray 'dotNETServer=TestServer.Test
Debug.Print str
Next
Dim arr(3) As String
arr(1) = "Pahee"
arr(2) = "Tharani"
arr(3) = "Rathan"
dotNETServer.SetArray (arr) 'This one causing the compile error which I mentioned earlier
更新: ::::::
We need to pass the array as reference in C#。在界面和方法中更改它
void SetArray(ref string[] arrayVal ); //ref added
答案 0 :(得分:3)
编组到适当的类型将解决您的问题。请注意
下的编组和参考关键字更改void ITest.SetArray([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VT_BSTR)] ref string[] arrayVal)
{
string[] stringArray1 = arrayVal;
}
我根据您的代码制作了此解决方案,并且您无法从VB6获取数据。如果以上解决方案对您不起作用,请尝试在此处查找适合您的应用的数组类型/子类型http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.110).aspx
答案 1 :(得分:2)
您的问题出在Vb6代码中:
dotNETServer.SetArray (arr)
这实际上是强制arr
按值传递,因为它被括号括起来而没有Call
个关键字。
你想这样做:
Call dotNETServer.SetArray(arr)
或
dotNETServer.SetArray arr