How to pass a VB6 string array [Assume, s =Array("a", "b", "c", "d")] to C# .Net through COM Interop?
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
Update: ::::::
We need to pass the array as reference in C#. Change it in the interface and method
void SetArray(ref string[] arrayVal ); //ref added
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;
}
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
Your issue was in the Vb6 code:
dotNETServer.SetArray (arr)
This is actually forcing arr
to be passed by value because it is enclosed by parentheses with no Call
keyword.
You want to do this:
Call dotNETServer.SetArray(arr)
or
dotNETServer.SetArray arr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With