2

I have a question on the COM interfaces exposed from an application. Can I have a COM interface where some parameters have default values similar to the one allowed by C++?

1 Answer 1

4

Default values can be defined with interface definition in IDL and are language agnostic. A method argument can be provided with a defaultvalue attribute, and then defaultvalue attribute article on MSDN states:

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

Note that only optional parameters can have defaultvalue attribute. This makes sense as non-optional parameter always comes with explicit value. This also means that any parameter having default value is actually a VARIANT parameter (effectively this is not true though: IDL compiler accepts defaultvalue attribute on non-VARIANT parameters).

Parameters with default values have restrictions on types:

The default value you specify for the parameter can be any constant, or an expression that resolves to a constant, that can be represented by a VARIANT. Specifically, you cannot apply the [defaultvalue] attribute to a parameter that is a structure, an array, or a SAFEARRAY.type.

Information about the default value stays on the IDL and the respective type library. It is the responsibility of the caller to read it and substitute at the time of the call. C++ clients #import'ing the type library will have to supply the value explicitly anyway. C# client adding such type library as a reference will extract default value and translate it into optional parameter with this default value and then will use it on the caller side of the call in such way that server sees no difference whether this value is default or explicitly given.

On the implementation side, a parameter with default value is identical to regular parameter with no default value defined: the actual value is always provided by the caller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.