Motti
Motti

Reputation: 114765

How to specify a default value for VARIANT_BOOL?

MS IDL has syntax for specifying a defaultvalue for parameters. I tried to specify a default value for a function that accepts a VARIANT_BOOL:

[id(42)] HRESULT Foo([in, defaultvalue(VARIANT_TRUE)] VARIANT_BOOL bar);

And got the following error message:

error MIDL2035 : constant expression expected

What is the correct syntax for specifying that the default value of bar should be VARIANT_TRUE?

Upvotes: 6

Views: 3124

Answers (2)

Motti
Motti

Reputation: 114765

Although one should not mix up bool, BOOL and VARIANT_BOOL it appears that in idl BOOL is interpreted as a VARIANT_BOOL value.

[id(42)] HRESULT Foo([in, defaultvalue(TRUE)] VARIANT_BOOL bar);

When called from VBScript with no parameter specified this reaches the C++ code as -1.

I'm not sure which way is more idiomatic TRUE or as @Hans suggested -1.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942020

VARIANT_TRUE is #defined in WTypes.h. You can't directly use it in your .idl. The common approach is to simply use the value directly, like it is done in mshtml.idl for example:

  [id(42)] HRESULT Foo([in, defaultvalue(-1)] VARIANT_BOOL bar);

Or you can add a #define to your .idl if you prefer, put it somewhere near the top:

#define VARIANT_TRUE -1
#define VARIANT_FALSE 0

Upvotes: 7

Related Questions