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
6
  • I like the JSON method: stackoverflow.com/questions/15649696/… Commented May 7, 2014 at 2: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. Commented May 7, 2014 at 2:18
  • @HansPassant, thank you very much. any idea for my question to send VB6 string array to C#? Commented May 7, 2014 at 2:29
  • 2
    I was talking about marshaling a string from vb6 to C#. That means "send". Commented May 7, 2014 at 2:34
  • Does this answer your question? Pass an array from vba to c# using com-interop Commented May 22, 2022 at 11:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

You are right. When I add ref key word to the array parameter. It is working. Just found. Thanks
The marshaling attribute is not required. It is the ref that fixes it.
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.