7

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
GSerg
  • 76,472
  • 17
  • 159
  • 346
RobinAtTech
  • 1,299
  • 3
  • 22
  • 48
  • I like the JSON method: http://stackoverflow.com/questions/15649696/how-can-i-pass-a-collection-of-objects-from-vb6-to-net – Jeremy Thompson May 07 '14 at 02:03
  • 4
    Just use a string array in VB6, Dim arr(42) As String. It is automatically marshaled to string[] if you have Option Base 0 in effect. If you want to monkey with Variant for some reason then you'll have to use *object* in C# and cast. – Hans Passant May 07 '14 at 02:18
  • @HansPassant, thank you very much. any idea for my question to send VB6 string array to C#? – RobinAtTech May 07 '14 at 02:29
  • 2
    I was talking about marshaling a string *from* vb6 *to* C#. That means "send". – Hans Passant May 07 '14 at 02:34
  • Does this answer your question? [Pass an array from vba to c# using com-interop](https://stackoverflow.com/questions/2027758/pass-an-array-from-vba-to-c-sharp-using-com-interop) – GSerg May 22 '22 at 11:44
  • @RobinAtTech the link in your update is dead. – SteveB Mar 17 '23 at 20:59

2 Answers2

3

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

pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • 1
    You are right. When I add ref key word to the array parameter. It is working. Just found. Thanks – RobinAtTech May 07 '14 at 03:42
  • The marshaling attribute is [not required](https://stackoverflow.com/questions/11468355/how-to-pass-a-long-array-from-vb6-to-c-sharp-thru-com#comment15144324_11468355). It is [the `ref`](https://stackoverflow.com/q/2027758/11683) that fixes it. – GSerg May 22 '22 at 11:39
1

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
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • VB6 is unable to pass an array by value. If it could, there wouldn't be the "Function or interface marked as restricted, or the function uses an automation type not supported in visual basic" error. Neither addition of `Call` nor removal of parentheses can change that. The fix is to [add `ref` to the C# side](https://stackoverflow.com/q/2027758/11683). – GSerg May 22 '22 at 11:52