Microsoft KB Archive/814912

= You receive a &quot;System.Runtime.InteropServices.COMException&quot; error message when you access a COM component in Visual Basic =

Article ID: 814912

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





SYMPTOMS
You may access a Component Object Model (COM) object from managed code and then call a method that has parameters of the type IUnKnown. When you do this by using Microsoft Visual Basic 2005 in Microsoft Visual Studio 2005 or by using Microsoft Visual Basic .NET in Microsoft Visual Studio .NET, you may receive the following error message:

Unhandled exception: System.Runtime.InteropServices.COMException: Type mismatch, occurred at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack) at MyRepro.Module1.Main.



CAUSE
When you use a late-binding mechanism in Visual Studio 2005 or in Visual Studio .NET, the runtime cannot marshal an object that has a value of NOTHING. In Visual Studio 2005 or in Visual Studio .NET, if an object variable contains NOTHING when it is encountered, the object variable is not changed and no instance is automatically created. If the object variable is set to NULL in a program that is written in Microsoft Visual C# .NET, this reproduces the same behavior. In the Microsoft .NET Framework, late binding is implemented by using the InvokeMember method of the Type class.



WORKAROUND
When you use the late-binding mechanism, you can avoid this behavior if you pass the parameters as UnKnownWrapper to methods that have parameters of type IUnKnown. The UnKnownWrapper constructor initializes a new instance of the UnknownWrapper class with the object that will be wrapped.

The following code sample wraps the invar object, the inoutvar object, and the outvar object that the marshaler must marshal as a COM variant type VT_UNKNOWN. ' This is the code sample.

'MyRepro is being imported. Imports MyRepro

Module Module1

' This method must be named &quot;Main&quot;. Sub Main

'This variable of type Object can hold data of any type. Dim CObj11 As Object

'AppObj holds object of type MyReproApp. Dim AppObj As MyReproApp

'The following Object variables can hold data of any type and can be used as parameters to method M_UNKNOWN. Dim invar As Object Dim inoutvar As Object Dim outvar As Object Dim retval As Object Dim COMNothing As Object COMNothing = New Runtime.InteropServices.UnknownWrapper(Nothing)

invar = COMNothing inoutvar = COMNothing outvar = COMNothing

AppObj = New MyReproApp CObj1 = AppObj.NewReproObj

retval = CObj1.Repro(invar, inoutvar, outvar)

End Sub

End Module



STATUS
This behavior is by design.



Steps to reproduce the behavior
If you have a COM component that is registered on your system, follow these steps:  Start Visual Studio 2005 or Visual Studio .NET. Create a new Visual Basic 2005 or Visual Basic .NET console application.  In the Module1.vb file, replace the existing code with the following code. ' This is a code sample.

'MyRepro is being imported. Imports MyRepro

Module Module1

' This method must be named &quot;Main&quot;. Sub Main

'This variable of type Object can hold data of any type. Dim CObj11 As Object

'AppObj holds object of type MyReproApp. Dim AppObj As MyReproApp

'The following Object variables can hold data of any type and can be used as parameters to method M_UNKNOWN. Dim invar As Object Dim inoutvar As Object Dim outvar As Object Dim retval As Object

AppObj = New MyReproApp CObj1 = AppObj.NewReproObj

retval = CObj1.Repro(invar, inoutvar, outvar)

End Sub

End Module  In Solution Explorer, right-click References. Click Add Reference. In the Add Reference dialog box, click the COM tab.</li> In the list, click to select the COM component that you have already registered, and then click OK.</li> On the Debug menu, click Start.</li></ol>

<div class="references_section">