I have an existing Interface API, which has an OUT, RETVAL parameter like below
HRESULT Test([out, retval] VARIANT_BOOL *pVal);
I was asked to add an optional IN parameter, hence I tried the below, since RETVAL parameter by rule should be the last parameter I was forced to put the optional IN parameter as a first parameter
HRESULT Test([in, optional, defaultvalue(VARIANT_FALSE)] VARIANT_BOOL optFlag, [out, retval] VARIANT_BOOL *pVal);
When I tried to call this API like below, with only the mandatory OUT parameter from a C++ component compiler errors out saying two parameters are required for this call
VARIANT_BOOL outFlag;
pInterface->Test(&outFlag);
Please let me know what I am missing here to achieve this combination.
MSDN on optional
IDL attribute:
The [optional] attribute is valid only if the parameter is of type VARIANT or VARIANTÂ *.
You are trying to apply it to VARIANT_BOOL
.
There is also a hint there on the order of parameters with such attributes:
The MIDL compiler accepts the following parameter ordering (from left-to-right):
- Required parameters (parameters that do not have the [defaultvalue] or [optional] attributes),
- Optional parameters with or without the [defaultvalue] attribute,
- Parameters with the [optional] attribute and without the [defaultvalue] attribute,
- [lcid] parameter, if any,
- [retval] parameter
IDL compiler issues a warning when compiling such method:
warning MIDL2401: [defaultvalue] is applied to a non-VARIANT and [optional]. Please remove [optional] : [ Parameter 'optFlag' of Procedure 'Test' ...
Nevertheless, attributes are compiled into type library and are available for importers. For instance, C# imports the method as:
bool Test(bool optFlag = false);
which should match your expectations. However this goes rather as a free bonus since type library importers are free to interpret the flags (esp. exceeding the documented limitations) at their discretion. C++ #import
does not generate a method with an optional parameter.
The C++ bindings generated by microsofts IDL compiler do not support the optional and/or default value feature.
Try it from C# or similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With