如何通过 COM Interop 将 VB6 字符串数组 [Assume, s =Array("a", "b", "c", "d")] 传递给 C#.Net?
How to pass a VB6 string array [Assume, s =Array("a", "b", "c", "d")] to C#.Net through COM Interop?
我尝试实现将 C# 字符串数组传递给 VB,并将 VB 字符串数组传递给 C#,如下所示 C#->VB 工作正常,但其他方式 (VB=>C#) 给出了一个名为 的编译错误函数或接口标记为受限,或者函数使用了 Visual Basic 不支持的自动化类型".下面是我的代码
I tried to implement passing C# string array to VB and VB string array to C# as below C#->VB working fine but other way (VB=>C#) giving a compile error called "Function or interface marked as restricted, or the function uses an automation type not supported in visual basic" . My code below
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
更新::::::::
我们需要在 C# 中将数组作为引用传递.在接口和方法中改一下
We need to pass the array as reference in C#. Change it in the interface and method
void SetArray(ref string[] arrayVal ); //ref added
编组到适当的类型将解决您的问题.注意下面的编组和 ref 关键字更改
Marshaling to appropriate type will solve your problem. Note marshaling and ref keyword change below
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
I made this solution based on your code and issue that you are not able to fetch data from VB6. If above solution does not work for you the do try finding the array type/subtype suitable for your application here http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.110).aspx
这篇关于将字符串数组从 VB6 传递到 C#.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!