Microsoft KB Archive/317273

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

Article ID: 317273

Article Last Modified on 2/3/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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

Additional information: No such interface supported

Note In this error message,  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.



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

Create Visual Basic .NET Application That Uses the Legacy Component
 Start Visual Studio .NET.</li> Create a new Visual Basic .NET console application. Module1 is created by default.</li> To add a reference to the legacy COM DLL that you created in the previous steps, follow these steps:  On the Project menu, click Add Reference.</li> On the COM tab, click Browse.</li> Locate the folder that contains the Visual Basic 6.0 ActiveX DLL, and then click the DLL.</li> In the Selected Components list, click the DLL, and then click OK.</li></ol> </li>  Delete the Module...End Module procedure, and then add the following code in the Module1 declarations section: Module Module1 Class Cls1 Sub F MsgBox(&quot;Hello&quot;) 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 </li> Press F5 to run the project. Notice that you receive the error message that is listed in the &quot;Symptoms&quot; section.</li></ol>

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(&quot;Hello&quot;) End Sub End Class

Keywords: kbtshoot kbprb kbvs2005swept kbvs2005doesnotapply KB317273

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.