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. CAUSEThe 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- Create a new Visual Basic 6.0 Microsoft ActiveX DLL
project. By default, Class1 is created.
- Rename the project as
CollectionFactory, and then rename the class as
clsVBACollection.
- 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 - On the File menu, click Make
CollectionFactory.dll.
- 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.
- In Solution Explorer, right-click
References and then click Add
Reference.
- In the Add Reference dialog box, click the
COM tab.
- Click Browse, and then locate
CollectionFactory.dll. Click OK.
Note In Visual Studio 2005, click CollectionFactory.dll, and then click OK. - 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
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" 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: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbvs2002sp1sweep kberrmsg kbMsg kbDLL kbCollections kbCollectionClass kbinterop kbAutomation kbAppCompatibility kbbug KB323737 kbAudDeveloper |
---|
|