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

Article translations Article translations
Article ID: 317030 - View products that this article applies to.
This article was previously published under Q317030
Expand all | Collapse all

On This Page

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.

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.
  1. 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 = "This is a String."
        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
    					
  2. Rename the project "VB6Component", and then save and compile the project. Close Visual Basic 6.0 (IDE).

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.
  1. 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.
  2. Drag a Button control and a Label control to the Form.
  3. From the Solution Explorer, right-click the References folder, and then click Add Reference.
  4. 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.
  5. Double-click the form, and then add the following lines of sample code at the beginning:
    Imports VB6Component
    Imports System.Text
    					
  6. From the View menu, click Other Windows, and then click Object Browser. Interop.VB6Component should appear on the list of items.
  7. Double-click to expand Interop.VB6Component and you should see the VB6Component namespace.
  8. Click to select the Test class. The Members pane of the object browser displays the function ReturnVariant.
  9. 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.
  10. 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 & ":" & Ex.Message)
            End Try
    					
  11. On the Build menu, click Build TestVBClient.
  12. On the Debug menu, click Start (or press F5).
  13. Click the button. The label is filled with the string "This is a string."

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.
  1. Create a new Windows Application project in C#. Form1 is created by default. Rename the project "TestCSClient".
  2. Drag a Button control and a Label control to the Form.
  3. From the Solution Explorer, right-click the References folder, and then click Add Reference.
  4. 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.
  5. Double-click the form, and then add the following lines of sample code at the beginning:
    using System.Text;
    using VB6Component;
    					
  6. 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 + ": " + Ex.Message);
    	}
    
    					
  7. On the Build menu, click Build TestCSClient.
  8. On the Debug menu, click Start (or press F5).
  9. Click the button. The label is filled with the string "This is a string."

Troubleshooting

If the lower bound of the returned array is set to any number other than zero (0), you receive the following error message:
"An unhandled exception of type 'System.InvalidCastException' occurred in AppName.exe"

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).

REFERENCES

Visual Studio.NET

Interoperating with Unmanaged Code

Properties

Article ID: 317030 - Last Review: December 6, 2006 - Revision: 3.5
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 6.0 Professional Edition
Keywords: 
kbvs2005swept kbvs2005applies kbhowtomaster kbinterop KB317030

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com