BUG: An exception error occurs when you pass VARIANT parameters by reference to an MFC IDispatch COM Component in Visual Studio .NET 2002 (312910)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

This article was previously published under Q312910

SYMPTOMS

When you pass VARIANT parameters by reference from managed code to a Microsoft Foundation Classes (MFC) Automation Server, you receive the following error message:
An exception 'System.Runtime.InteropServices.COMException' has occurred.

CAUSE

This problem occurs because the System.Type.InvokeMember method only passes parameters by value to COM methods. This is the default behavior.

For example, if you use a method such as the following to pass a VARIANT parameter by reference, an exception error occurs:
HRESULT _stdcall Method1(
                [in, out] VARIANT* inoutVal, 
                [out] VARIANT* outVal, 
                [out, retval] VARIANT* retVal);
Currently, you cannot change this default behavior directly by using the System.Type class.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and the tools that are used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, visit the following Microsoft Web site:For additional information about the support options available from Microsoft, visit the following Microsoft Web site: To work around this problem, you can use the System.Reflection.ParameterModifier object to change the parameter type manually to VT_BYREF. You can then use late binding to invoke the method.

The following sample C# code describes this workaround:
using MyMFCLibrary;
using System.Reflection;

public class MyClass
{
  MyMFCAutomationServer objTest = new MyMFCAutomationServerDoc();
 
 object [] arglist = { "Hello World!" };

 ParameterModifier pm = new ParameterModifier(1);
 
 //Set the VT_BYREF flag on the first parameter.
 pm[0] = true; 

 //Create an array of ParameterModifier objects, and then put in your element.
 ParameterModifier [] pmArray = { pm };
 
 //Use late binding and call the method.
 objTest.GetType().InvokeMember("VariantByRef",
                                 BindingFlags.InvokeMethod, 
                                 null, 
                                 objTest, 
                                 arglist, 
                                 pmArray, 
                                 null, 
                                 null);
}

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

The MFC implementation of the IDispatch::Invoke method expects to see the flags VT_VARIANT | VT_BYREF specified in the type tag field vt. However, the Type.InvokeMember method does not supply this flag combination to the IDispatch::Invoke method. When the Microsoft .NET Framework prepares the parameter list for marshaling, it inserts the flag VT_EMPTY instead of the flags VT_VARIANT | VT_BYREF.

REFERENCES

Nathan, Adam. .NET and COM: The Complete Interoperability Guide. Indianapolis, IN: Sams Publishing, 2002.

Modification Type:MinorLast Reviewed:8/23/2006
Keywords:kbvs2002sp1sweep kbAutomation kbbug kbpending KB312910 kbAudDeveloper kbAudITPRO