Microsoft KB Archive/131052

= SAMPLE: OPTARG: Optional Parameters with Automation =

Article ID: 131052

Article Last Modified on 8/9/2004

-

APPLIES TO

 Microsoft OLE 2.03 Microsoft OLE 4.0, when used with:  Microsoft Windows NT 4.0

 Microsoft Windows 95 

-



This article was previously published under Q131052



SUMMARY
OPTARG is an OLE Automation server application that demonstrates automation methods with optional parameters and variable number of parameters. It implements an Add method that takes optional parameters and a Multiply method that takes a variable number of parameters.

See the README.TXT included with the sample for details to compile and run.

<div class="moreinformation_section">

MORE INFORMATION
The following file is available for download from the Microsoft Download Center:

Optarg.exe

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Optional parameters are implemented by marking the parameter with the optional attribute and by using VARIANT as the type of the parameter. If the controller omits a parameter, the method will be passed a VARIANT with VARIANT.vt = VT_ERROR and VARIANT.scode = DISP_E_PARAMNOTFOUND.

Variable parameters are implemented by marking the method with the vararg attribute and by providing a parameter of type SAFEARRAY(variant). This array will contain the variable number of parameters passed by the controller.

NOTE: vararg methods do not have the upper limit of parameters that methods with optional parameters do.

Because of a problem in the OLE dlls, invoking the Multiply method will cause an exception in the 32-bit OLE that shipped with Windows NT 3.5 and a GP fault in 16-bit OLE 2.02 and 2.03. This problem has been fixed in NT 3.51 and Windows 95.

The problem occurs if a vararg method has a retval parameter. For example, the following will fail: [vararg] HRESULT Multiply([in] short RequiredInteger,                     [in] SAFEARRAY(variant) VariableNumberOfIntegers,                      [out, retval] long* retval); The problem does not occur if the vararg method doesn't have a retval parameter. For example, the following will currently work: [vararg] HRESULT Multiply([in] short RequiredInteger,                     [in] SAFEARRAY(variant) VariableNumberOfIntegers); [vararg] long Multiply([in] short RequiredInteger,                     [in] SAFEARRAY(variant) VariableNumberOfIntegers);

Additional query words: Optarg arguments 3.50 4.00 kbfile

Keywords: kbautomation kbsample KB131052

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.