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.
SYMPTOMSWhen 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.
CAUSEWhen 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); RESOLUTIONTo 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.
Modification Type: | Minor | Last Reviewed: | 9/13/2005 |
---|
Keywords: | kbvs2002sp1sweep kbControl kbCOMInterop kbbug KB815633 kbAudDeveloper |
---|
|