Passing array from .NET DLL to VB 6 application
Lets say we have a class library developed in .NET which returns an array of objects. When we use this .NET DLL in any VB 6 application an error throws saying “Type mismatch -error”. Even if the .NET DLL is registered for COM interop the error will throw while accessing array objects.
To resolve this issue we need to create a COM class in .NET. You can add a COM class to .NET project using Add->New Item->COM Class.
Here is an example showing usage of COM class.
[VB.NET CODE STARTS]
_ Public Class COMCLass1 #Region "COM GUIDs" ' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. Public Const ClassId As String = "81960d64-b2fd-4344-9224-90376eb95d7a" Public Const InterfaceId As String = "621c203c-557a-49eb-814c-e67a8daa9376" Public Const EventsId As String = "2345b1c4-e95f-4d23-b007-dd276d349578" #End Region ' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Private _item As Integer Public Property Items() As Integer Get Return _item End Get Set(ByVal value As Integer) _item = value End Set End Property End Class _ Public Class COMCLass2 #Region "COM GUIDs" ' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. Public Const ClassId As String = "bd0ae776-0df4-43ab-bb17-bd51d9cfa69f" Public Const InterfaceId As String = "9845f800-6bce-4a78-9f24-e263d7d47e74" Public Const EventsId As String = "7be9b049-1952-416a-9474-72cb5e17534d" #End Region ' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Private items() As COMCLass1 Public Function GetItemArray() As COMCLass1 ReDim items(1) items(0) = New ComClass1 items(1) = New ComClass1 items(0).Items = 1 items(1).Items = 2 Return items End Function End Class
[VB.NET CODE ENDS]
Add the reference of .NET DLL in VB 6.
[VB 6 CODE STARTS]
Dim comObj As COMCLass2 Set comObj = New COMCLass2 Dim comObj2() As COMCLass1 comObj2 = comObj.GetItemArray() Dim i As Integer For i = 0 To Unbound(comObj2) ' get each value Next
[VB 6 CODE ENDS]
This way we can pass array objects from .NET to VB 6 application.