FIX: SafeArrayTypeMismatchException Error Message When You Invoke a COM Method (317048)



The information in this article applies to:

  • Microsoft Windows .NET Framework 1.1
  • Microsoft .NET Framework 1.0

This article was previously published under Q317048

SYMPTOMS

When you invoke a method that contains the following signature on a Microsoft Visual Basic 6.0 Component Object Model (COM) component from a managed client
Public Function Moo(ByRef Ary() As Object)
				
you may receive the following SafeArrayTypeMismatchException error message:
An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in vbMannagedConApp.exe

CAUSE

The array of managed objects is passed as a SAFEARRAY of VARIANTs, but the unmanaged method expects a SAFEARRAY of IDispatch pointers.

RESOLUTION

To work around this problem, use the following sample code:
Sub Main()
	dim objs(9) as Object
	dim dispArray(9) as System.Runtime.InteropServices.DispatchWrapper
	dim i as integer

	for i = 0 to 9
		objs(i) = New Object()
		dispArray(i) = New DispatchWrapper(objs(i))
	next

	Dim Obj As Project1.Class1Class = New Project1.Class1()
	Obj.Moo(CType(dispArray, Object()))
End Sub
				

STATUS

This bug was corrected in .NET Framework (2003|1.1).

MORE INFORMATION

Steps to Reproduce Behavior

Add a reference to the COM server for a managed application, and then call the Moo method from a managed application as follows:
Dim Obj As VB6Server.Class1 = New VB6Server.Class1()
Obj.Moo(New Object() {New String("test String")})
				

Modification Type:MinorLast Reviewed:5/28/2003
Keywords:kbfix kbCOMInterop kbpending kbprb KB317048