You are currently offline, waiting for your internet to reconnect

You receive a "System.Runtime.InteropServices.COMException" error message when you access a COM component in Visual Basic

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 MyReproModule Module1' This method must be named "Main".    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 SubEnd Module
STATUS
This behavior is by design.
MORE INFORMATION

Steps to reproduce the behavior

If you have a COM component that is registered on your system, follow these steps:
  1. Start Visual Studio 2005 or Visual Studio .NET. Create a new Visual Basic 2005 or Visual Basic .NET console application.
  2. In the Module1.vb file, replace the existing code with the following code.
    ' This is a code sample. 'MyRepro is being imported.Imports MyReproModule Module1' This method must be named "Main".    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 SubEnd Module
  3. In Solution Explorer, right-click References.
  4. Click Add Reference.
  5. In the Add Reference dialog box, click the COM tab.
  6. In the list, click to select the COM component that you have already registered, and then click OK.
  7. On the Debug menu, click Start.
REFERENCES
For more information about default marshaling for objects, visit the following Microsoft Developer Network (MSDN) Web site:
Properties

Article ID: 814912 - Last Review: 12/03/2007 18:51:00 - Revision: 2.6

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

  • kbconsole kbcomservices kbdebug kbprb kbcominterop kbvs2005applies kbvs2005swept KB814912
Feedback
Asimov.clickstreamTracker.init();