Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare IN, OPTIONAL parameter along with OUT, RETVAL parameter in IDL

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.

like image 700
Titus Avatar asked Oct 17 '25 06:10

Titus


2 Answers

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.

like image 127
Roman R. Avatar answered Oct 19 '25 23:10

Roman R.


The C++ bindings generated by microsofts IDL compiler do not support the optional and/or default value feature.

Try it from C# or similar.

like image 34
Yakk - Adam Nevraumont Avatar answered Oct 19 '25 21:10

Yakk - Adam Nevraumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!