BUG: "InteropServices.COMException" error message occurs when you pass ByRef Parameters by using late binding (815633)



The information in this article applies to:

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

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.

MORE INFORMATION

Steps to Reproduce the Problem

Create a COM Component

  1. Run Visual Basic 6.0.
  2. On the File menu, click New Project.
  3. Select the ActiveX DLL project in the project list and then click OK.

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

Create a Visual Basic .NET Application

  1. Run Visual Studio .NET.
  2. On the File menu, point to New and then click Project.
  3. Select Visual Basic Projects under the Project Types section. Click Console Application under the Templates section.
  4. Name the project TestRepro and then click OK.

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

    You receive the error message in the "Symptoms" section of this article.

Modification Type:MinorLast Reviewed:9/13/2005
Keywords:kbvs2002sp1sweep kbControl kbCOMInterop kbbug KB815633 kbAudDeveloper