Microsoft KB Archive/154039

= How to pass optional arguments to a method on an ActiveX Control =

Article ID: 154039

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li> Microsoft Visual C++ .NET 2002 Standard Edition</li> Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

-

<div class="notice_section">

This article was previously published under Q154039

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
Many automation methods on ActiveX Controls do not require that all possible arguments are passed with each call. Arguments that are not required are called optional arguments. In MFC, it is possible to add a method to a custom control that accepts optional arguments, but there are a few rules that must be followed. This article outlines these rules, and provides step-by-step instruction on how to add such a method to a control.

<div class="moreinformation_section">

MORE INFORMATION
MFC's implementation of IDispatch accepts optional arguments according to the following rules: <ol> Optional arguments must appear at the end of the parameter list. If an optional argument is omitted when calling the method, all arguments to the right of this one must also be omitted. This is similar to the C++ rules for calling functions with Default values for parameters.</li>  Optional arguments must be declared as VARIANTs. If a parameter is omitted, the calling program must pass a VARIANT initialized as: VARIANT.vt = VT_ERROR VARIANT.scode = DISP_E_PARAMNOTFOUND. This will be done automatically by Visual Basic. However, in Visual C++ you must create and initialize the VARIANT yourself.NOTE: If using Visula Studio.NET do the following: </li>  You must edit the IDL file generated for the control and mark the parameters as optional. This is done by adding the [optional] tag as shown here: [id(1)] void MyOpt([optional] VARIANT param1,           [optional] VARIANT param2); </li></ol>

To add a method that supports optional arguments, follow these steps: <ol> Using ClassWizard, add a new method to the control. Be sure to pick VARIANT as the type for each optional argument, and be sure the optional arguments appear last in the parameter list.

NOTE: If using Visual Studio .NET do the following:

In Class View, expand the project node to display the interface that you want to add the method. Click Add, and then click Add Method. Pick VARIANT as the type for each optional argument, and verify that the optional arguments appear last in the parameter list.</li> <li>Edit the ODL file, and mark these parameters with the [optional] tag.</li> <li> Add the appropriate functionality to the method and rebuild the control.

For example, the MyOpt method defined in ODL syntax above could be called from Visual Basic in the following three forms: <pre class="fixed_text">  myocx.MyOpt 2, 3  'passing both args myocx.MyOpt 2    'omit one arg myocx.MyOpt      'omit both args </li></ol>

<div class="references_section">