Microsoft KB Archive/317030

= How to handle arrays returned as a Variant from a Visual Basic COM object =

Article ID: 317030

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q317030





IN THIS TASK
SUMMARY
 * Create a COM Component in Visual Basic 6.0
 * Create a Test Client in Visual Basic .NET
 * Create a Test Client in C#
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article details how to handle the returned array when you make a function call to a Component Object Model (COM) component that returns an array contained in a Variant.

In Visual Basic 6.0, Variant is the default data type. In Visual Basic .NET or in Visual Basic 2005, Object is the default data type, and the Variant data type does not exist.

back to the top

Create a COM Component in Visual Basic 6.0
This article explains how to create a Visual Basic 6.0 component that returns a Variant that contains a Byte array. This article also includes the Visual Basic .NET or Visual Basic 2005 client application and the C# client application to demonstrate how to retrieve the values from the Variant.   Create a Visual Basic 6.0 ActiveX DLL project. Rename the class Test, and then paste the following lines of code in the Class module: Public Function ReturnVariant As Variant

Dim byteArr As Byte Dim Str As String Str = &quot;This is a String.&quot; ReDim byteArr(0 To Len(Str) - 1) For i = 1 To Len(Str) byteArr(i - 1) = CByte(Asc(Mid$(Str, i, 1))) Next i   ReturnVariant = byteArr

End Function  Rename the project &quot;VB6Component&quot;, and then save and compile the project. Close Visual Basic 6.0 (IDE).

back to the top

Create a Test Client in Visual Basic .NET or in Visual Basic 2005
Write a Visual Basic .NET or a Visual Basic 2005 client application that uses the component you created in the previous section to use the data returned from the component.  Create a New Windows Application project in Visual Basic .NET or in Visual Basic 2005. Form1 is created by default. Rename the project to TestVBClient. Drag a Button control and a Label control to the Form. From the Solution Explorer, right-click the References folder, and then click Add Reference.</li> In the Add Reference dialog box, click the COM tab. Click Browse, and then locate VB6Component.dll. The component should appear in the selected components list. Click OK. You should see a reference to the VB6Component added to the references list.</li>  Double-click the form, and then add the following lines of sample code at the beginning: Imports VB6Component Imports System.Text </li> From the View menu, click Other Windows, and then click Object Browser. Interop.VB6Component should appear on the list of items.</li> Double-click to expand Interop.VB6Component and you should see the VB6Component namespace.</li> Click to select the Test class. The Members pane of the object browser displays the function ReturnVariant.</li> Click the ReturnVariant function, and then refer to the data type that the function returns in the lower pane. The data type should be Object.</li>  Switch to the design view for the Form, double-click the Button control, and then paste the following lines of code in the Click_Event handler: Dim objRetVal As Object Dim typ As Type Dim str As String Dim byteArr As Byte Try

Dim testClass As New VB6Component.TestClass objRetVal = testClass.ReturnVariant byteArr = CType(objRetVal, Array) Label1.Text = ASCIIEncoding.ASCII.GetString(byteArr) Catch Ex As Exception MessageBox.Show(Ex.Source & &quot;:&quot; & Ex.Message) End Try </li> On the Build menu, click Build TestVBClient.</li> On the Debug menu, click Start (or press F5).</li> Click the button. The label is filled with the string &quot;This is a string.&quot;</li></ol>

back to the top

Create a Test Client in C#
Write a C# client application that uses the component you created previously to use the data returned from the component.  Create a new Windows Application project in C#. Form1 is created by default. Rename the project &quot;TestCSClient&quot;.</li> Drag a Button control and a Label control to the Form.</li> From the Solution Explorer, right-click the References folder, and then click Add Reference.</li> In the Add Reference dialog box, click the COM tab. Click Browse, and then locate VB6Component.dll. The component should appear in the list of selected components. Click OK. You should see a reference to the VB6Component added to the list of references.</li>  Double-click the form, and then add the following lines of sample code at the beginning: using System.Text; using VB6Component; </li> <li> Switch to the design view for the form, double-click the Button control, and then paste the following lines of sample code in the Click_Event handler: object objRetVal; byte[] byteArr; try {       TestClass tc = new TestClass; objRetVal = tc.ReturnVariant; byteArr = (byte[]) objRetVal; label1.Text = ASCIIEncoding.ASCII.GetString(byteArr); }       catch(Exception Ex) {       MessageBox.Show(Ex.Source + &quot;: &quot; + Ex.Message); }

</li> <li>On the Build menu, click Build TestCSClient.</li> <li>On the Debug menu, click Start (or press F5).</li> <li>Click the button. The label is filled with the string &quot;This is a string.&quot;</li></ol>

back to the top

Troubleshooting
If the lower bound of the returned array is set to any number other than zero (0), you receive the following error message:

&quot;An unhandled exception of type 'System.InvalidCastException' occurred in AppName.exe&quot;

Additional information: Specified cast is not valid.

This error occurs because all arrays in .NET have a lower bound of zero (0). The lower bound of the array that is returned from the Visual Basic 6.0 application also must be set to zero (0).

back to the top