How to handle arrays returned as a Variant from a Visual Basic COM object (317030)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
This article was previously published under Q317030 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 = "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
- Rename the project "VB6Component", 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.
- 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.
- Double-click the form, and then add the following lines of sample code at the beginning:
Imports VB6Component
Imports System.Text
- From the View menu, click Other Windows, and then click Object Browser. Interop.VB6Component should appear on the list of items.
- Double-click to expand Interop.VB6Component and you should see the VB6Component namespace.
- Click to select the Test class. The Members pane of the object browser displays the function ReturnVariant.
- 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.
- 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
- On the Build menu, click Build TestVBClient.
- On the Debug menu, click Start (or press F5).
- Click the button. The label is filled with the string "This is a string."
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 "TestCSClient".
- 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.
- 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.
- Double-click the form, and then add the following lines of sample code at the beginning:
using System.Text;
using VB6Component;
- 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);
}
- On the Build menu, click Build TestCSClient.
- On the Debug menu, click Start (or press F5).
- Click the button. The label is filled with the string "This is a string."
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:
"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).
back to the top
REFERENCESVisual Studio.NETInteroperating with Unmanaged Code
back to the top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbHOWTOmaster kbinterop KB317030 kbAudDeveloper |
---|
|