Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 731
Motti Avatar asked Sep 25 '11 10:09

Motti


2 Answers

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
like image 51
Hans Passant Avatar answered Nov 09 '22 10:11

Hans Passant


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.

like image 39
Motti Avatar answered Nov 09 '22 09:11

Motti