Microsoft KB Archive/815633

= BUG: &quot;InteropServices.COMException&quot; error message occurs when you pass ByRef Parameters by using late binding =

Article ID: 815633

Article Last Modified on 9/13/2005

-

APPLIES TO


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

-





Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.



SYMPTOMS
When you call a COM component in Visual Basic .NET, and you use late binding, you may receive the following error message:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll. Additional information: Type mismatch.

This behavior occurs when all the following conditions are true:
 * The method that is called is a Public method in the COM component.
 * The method takes a ByRef parameter of type Object.
 * You use late binding to call the COM component.



CAUSE
When you run an application, the .NET Framework late binding mechanism tries to pass the ByRef parameters to the COM component as objects. The COM component expects the parameters to be of the type IDispatch. The .NET Framework raises a type mismatch exception. This occurs because the .NET Framework cannot convert the uninitialized objects to the IDispatch type. The early binding signature of the method in the COM component follows: [id(0x60030000)] HRESULT MyFunc([in, out] IDispatch** var1);



RESOLUTION
To resolve this problem, convert the objects to the IDispatch type by using the DispatchWrapper object. The DispatchWrapper object forces the objects to be marshaled out as VT_DISPATCH. Assign the object to the DispatchWrapper object. Do this instead of assigning the object to Nothing. You can do this if you pass Nothing as a parameter to the constructor of DispatchWrapper.

The following code demonstrates how to complete this: Dim obj As Object obj = New Project1.Class1 Dim inval As Object 'Marshal the object before passing it to the method. inVal = New System.Runtime.InteropServices.DispatchWrapper(Nothing) obj.MyFunc(inval)



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Create a COM Component
 Run Visual Basic 6.0. On the File menu, click New Project. Select the ActiveX DLL project in the project list and then click OK.

By default, Class1 is created.  Add the following code to the General Declarations section of Class1: Public Sub MyFunc(var1 As Object) End Sub  On the File menu, click Make Project1.dll to create the component.</li> Close Visual Basic 6.0.</li></ol>

Create a Visual Basic .NET Application
<ol> Run Visual Studio .NET.</li> On the File menu, point to New and then click Project.</li> Select Visual Basic Projects under the Project Types section. Click Console Application under the Templates section.</li> Name the project TestRepro and then click OK.

By default, Module1.vb is created.</li>  Add the following code in the Main method: Dim x As Object x = New Project1.Class1 Dim inval As Object x.MyFunc(inval) </li> On the Debug menu, click Start to run the application.

You receive the error message in the &quot;Symptoms&quot; section of this article.</li></ol>

Keywords: kbbug kbcontrol kbcominterop kbvs2002sp1sweep KB815633

-

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

© Microsoft Corporation. All rights reserved.