Unhandled exception when you pass non-public class object to COM DLL in Visual Basic .NET (317273)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q317273

SYMPTOMS

When you pass a non-public Visual Basic .NET class object to a Visual Basic Component Object Model (COM) dynamic-link library (DLL), you may receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in ConsoleApplication1.exe

Additional information: No such interface supported
Note In this error message, ConsoleApplication1.exe is the name of your application .exe file.

CAUSE

By default, the Visual Basic .NET class is not public. Classes that do not specify an access modifier are declared as Friend by default.

RESOLUTION

To resolve this problem, make the Visual Basic .NET class public.

MORE INFORMATION

Steps to Reproduce Behavior

Create Legacy Component in Visual Basic 6.0

  1. Open Microsoft Visual Basic 6.0.
  2. Create a new Visual Basic 6.0 ActiveX DLL project. Class1 is created by default.
  3. Add the following code in the general declarations section of Class1:
    Public Sub LegacySub(ByRef arg As Object)
        arg.f
    End Sub
    					
  4. Save and compile the project.

Create Visual Basic .NET Application That Uses the Legacy Component

  1. Start Visual Studio .NET.
  2. Create a new Visual Basic .NET console application. Module1 is created by default.
  3. To add a reference to the legacy COM DLL that you created in the previous steps, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Browse.
    3. Locate the folder that contains the Visual Basic 6.0 ActiveX DLL, and then click the DLL.
    4. In the Selected Components list, click the DLL, and then click OK.
  4. Delete the Module...End Module procedure, and then add the following code in the Module1 declarations section:
    Module Module1
        Class Cls1
            Sub F()
                MsgBox("Hello")
            End Sub
        End Class
    
        Sub Main()
            Dim Obj As New Project1.Class1()    'Make sure that the ProgID matches the
                                                'legacy component that you created earlier.
            Dim Obj1 As Cls1                     
                                                
            Obj1 = New Cls1()
            Obj.LegacySub(Obj1)
        End Sub
    End Module
    					
  5. Press F5 to run the project. Notice that you receive the error message that is listed in the "Symptoms" section.

Resolution

Modify the code in Module1 as follows:
Module Module1
    Sub Main()
        Dim Obj As New Project1.Class1()
        Dim Obj1 As Cls1

        Obj1 = New Cls1()
        Obj.LegacySub(Obj1)
    End Sub
End Module

Public Class Cls1
    Sub F()
        MsgBox("Hello")
    End Sub
End Class
				

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005doesnotapply kbtshoot kbvs2005swept kbprb KB317273