BUG: You receive a "System.InvalidCastException" error message when you pass a Collection from a .NET-connected application to Visual Basic 6.0 components (323737)



The information in this article applies to:

  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic 2005

This article was previously published under Q323737

SYMPTOMS

When you try to pass a Collection object from Microsoft Visual Basic .NET to Visual Basic 6.0 components, you may receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in ApplicationName.exe
Additional information: Specified cast is not valid.
Note In Visual Studio 2005, you receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in ConsoleApplication3.exe
Additional information: Unable to cast object of type 'Microsoft.VisualBasic.Collection' to type 'VBA.Collection'.
If you examine the type of the collection object that Visual Basic .NET or Visual Basic 2005 expects, you find that the VBA.Collection type is expected instead of the Microsoft.VisualBasic.Collection type. If you change your code to pass a collection object of the type VBA.Collection, you receive the following error message on the line of code where you try to create a new instance of the VBA.Collection class:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ApplicationName.exe
Additional information: COM object with CLSID {A4C4671C-499F-101B-BB78-00AA00383CBB} is either not valid or not registered.
This problem also occurs in other Microsoft .NET Framework supported languages such as Microsoft Visual C# .NET or Microsoft Visual C# 2005.

CAUSE

The InvalidCastException occurs because Microsoft.VisualBasic.Collection is not compatible with VBA.Collection. You receive COMException error because VBA.Collection is designed in such a way that only a Visual Basic 6.0 application can create an instance of the VBA.Collection class. You cannot create an instance of VBA.Collection outside a Visual Basic 6.0 application.

WORKAROUND

To work around this issue, create a VBA.Collection object in a Visual Basic 6.0 application, and then return it to the Visual Basic .NET or Visual Bsic 2005 application. To do this, you can create a new Visual Basic 6.0 DLL or add a new method in the existing DLL.

Note The index of the Collection object in Visual Basic .NET or Visual Basic 2005 is base 1. However, the index of Visual Basic 6.0 is base 0. Therefore, you may have to modify the Visual Basic 6.0 DLL to use 1 as the base index for your collection instead of 0.

Create New Visual Basic 6.0 DLL that Returns Collection
  1. Create a new Visual Basic 6.0 Microsoft ActiveX DLL project. By default, Class1 is created.
  2. Rename the project as CollectionFactory, and then rename the class as clsVBACollection.
  3. Add the following code to the clsVBACollection class:
    ' Function to create a new object of VBA Collection
    Public Function CreateVBACollection() As Collection
    
       ' Define a variable of type Collection
       Dim col As Collection
    
       ' Create New Collection object
       Set col = New Collection
    
       ' Return new Collection Object
       Set CreateVBACollection = col
    End Function
  4. On the File menu, click Make CollectionFactory.dll.
  5. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Console Application project by using Visual Basic .NET or Visual Basic 2005. By default, Module1.vb is created.
  6. In Solution Explorer, right-click References and then click Add Reference.
  7. In the Add Reference dialog box, click the COM tab.
  8. Click Browse, and then locate CollectionFactory.dll. Click OK.

    Note In Visual Studio 2005, click CollectionFactory.dll, and then click OK.
  9. Replace the Sub Main method with the following code:
       Sub Main()
          ' Create new instance of clsVBSCollection
          Dim objVBACollection As New CollectionFactory.clsVBACollectionClass()
    
          ' Variable to store the collection object returned by CollectionFactory
          Dim col As VBA.CollectionClass
    
          ' Get new VBA.Collection object
          col = objVBACollection.CreateVBACollection()
    
          ' Use the collection as you typically would
          col.Add("Microsoft")
       End Sub

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. In Visual Basic 6.0, create a new ActiveX DLL Project. By default, Class1 is created.
  2. Add the following code to Class1:
    Public Function GetCollection(col As Collection) As Variant
       GetCollection = col(1)
    End Function
  3. On the Project menu, click Properties. Rename the project as TestCollection.
  4. On the File menu, click Make TestCollection.dll.
  5. In Visual Studio .NET or Visual Studio 2005, create a new Console Application project by using Visual Basic .NET or Visual Basic 2005. By default, Module1.vb is created.
  6. In Solution Explorer, right-click References, and then click Add Reference.
  7. In the Add Reference dialog box, click the COM tab.
  8. Click Browse, and then locate TestCollection.dll. Click OK.

    Note In Visual Studio 2005, click TestCollection.dll, and then click OK.
  9. Replace the Sub New method with the following code:
       Sub Main()
          ' Create New Microsoft.VisualBasic.Collection object
          Dim col As New Collection()
          Dim objTestCol As New TestCollection.Class1Class()
          Dim objRetVal As Object
    
          ' Add item to collection
          col.Add("Hello World")
    
          ' Pass collection as parameter
          objRetVal = objTestCol.GetCollection(col)
       End Sub
    
  10. On the Debug menu, click Start to run the application. You receive the first error message as mentioned in the "Symptoms" section. Click Continue.
  11. Replace
       Dim col As New Collection()
    with the following code:
       Dim col As New VBA.CollectionClass()
  12. On the Debug menu, click Start to run the application. You receive the second error message that is mentioned in the "Symptoms" section.

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

316163 PRB: Error message when you attempt to build a Class Library project: COM Interop registration failed


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbvs2002sp1sweep kberrmsg kbMsg kbDLL kbCollections kbCollectionClass kbinterop kbAutomation kbAppCompatibility kbbug KB323737 kbAudDeveloper