INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...I have tons of books, have book marked tons of tutorials, which have helped, but this forum has answered those "impossible to find" solutions. I am thrilled with this site..."
Geography
Where in the world do Tek-Tips members come from?
|
Microsoft: Visual C++ FAQ
|
ActiveX control
|
How to create ActiveX with optional parameters
Posted: 7 Jun 00
|
As taken from the MSDN library
HOWTO: Pass Optional Arguments to MFC ActiveX Controls ID: Q154039
-------------------------------------------------------------------------------- The information in this article applies to:
The Microsoft Foundation Classes (MFC), included with: Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1, 4.2, 5.0, 6.0
--------------------------------------------------------------------------------
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.
MORE INFORMATION MFC's implementation of IDispatch accepts optional arguments according to the following rules:
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.
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: This implies that the method must contain code that checks the VARIANT to see if it contains actual data or is just a place holder.
You must edit the ODL 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); If you don't add the [optional] tag, Visual Basic will put up a message box indicating "Parameter Not Optional" when you call the method with one or more parameters omitted.
To add a method that supports optional arguments, follow these steps:
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.
Edit the ODL file, and mark these parameters with the [optional] tag.
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:
myocx.MyOpt 2, 3 'passing both args myocx.MyOpt 2 'omit one arg myocx.MyOpt 'omit both args
|
Back to Microsoft: Visual C++ FAQ Index
Back to Microsoft: Visual C++ Forum
My FAQ Archive
Email This FAQ To A Friend |
|
 |
|