Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Optional IDL Parameters and C++

868 views
Skip to first unread message

Sharon Fujita

unread,
Apr 15, 2002, 2:12:46 PM4/15/02
to
I want to add an optional parameter to an interface method so that I don't
break existing code. Here is my IDL (ApplyPrefs is the new parameter):

[id(1), helpstring("Retrieve recordset by Vehicle Type")]
HRESULT LookupByVehicleType([in] BSTR TemplateName,
[in] BSTR TemplateVersion,
[in] IVehicleType* VehicleType,
[in] VARIANT_BOOL singleMatch,
[in, defaultvalue(0)] VARIANT_BOOL ApplyPrefs,
// I added this
// [in, optional, defaultvalue(0)] VARIANT_BOOL
ApplyPrefs, // also tried this
// [in, optional ] VARIANT_BOOL ApplyPrefs,
// also tried this
[out, retval] IDispatch** AdoRecordset);

Now my C++ code gives me a compile error (C2660) saying the function does
not take 4 parameters. Am I doing something wrong or does C++ just not
support optional parameters?
Existing VB code still compiles.

Mike Philips

unread,
Apr 15, 2002, 2:26:27 PM4/15/02
to
Use VARIANT for optional parameters.

Mike
"Sharon Fujita" <sfu...@northropgrumman.com> wrote in message
news:Ov6uskK5BHA.1892@tkmsftngp05...

Igor Tandetnik

unread,
Apr 15, 2002, 4:12:27 PM4/15/02
to
Optional parameters are only good for late binding (calling though
IDispatch::Invoke). You have to specify all parameters for early
binding - otherwise, there's no way to tell where on the stack
parameters end and garbage begins.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"Sharon Fujita" <sfu...@northropgrumman.com> wrote in message
news:Ov6uskK5BHA.1892@tkmsftngp05...

Craig Powers

unread,
Apr 15, 2002, 4:56:17 PM4/15/02
to
Mike Philips wrote:
>
> Use VARIANT for optional parameters.

VB works fine with non-VARIANT optional parameters, and the added
type safety is nice in cases where it's not important to know whether
or not a parameter has been specified.

Craig Powers

unread,
Apr 15, 2002, 5:00:30 PM4/15/02
to

C++ supports optional parameters, but it won't work for a raw procedure
and AFAIK but the C++ wrappers generated from your IDL aren't set up
that way.

For the non-raw procedure, you'd have a declaration something like
this (modified for brevity):

IDispatch** LookupByVehicleType(BSTR, BSTR, IVehicleType*,
VARIANT_BOOL, VARIANT_BOOL ApplyPrefs=0);

dirk

unread,
Apr 15, 2002, 8:51:03 PM4/15/02
to
However from the midl reference:

The [optional] attribute is valid only if the parameter is of type
VARIANT or VARIANT *.

Also, from the ODL reference:

All subsequent parameters of the function must also be optional.

Dirk

"Craig Powers" <eni...@hal-pc.org> wrote in message
news:3CBB3E71...@hal-pc.org...

Igor Tandetnik

unread,
Apr 16, 2002, 9:38:17 AM4/16/02
to
Yeah, but there's that [defaultvalue] attribute that introduces an
optional parameter, too. Such a parameter can be strongly typed.

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"dirk" <dha...@hotmail.com> wrote in message
news:u8RCKDO5BHA.2072@tkmsftngp02...

Craig Powers

unread,
Apr 16, 2002, 9:54:27 AM4/16/02
to
dirk wrote:
>
> However from the midl reference:
>
> The [optional] attribute is valid only if the parameter is of type
> VARIANT or VARIANT *.

Yes. VB doesn't seem to mind, though.

I started doing this before I realized I could specify defaultvalue
without specifying optional.

> Also, from the ODL reference:
>
> All subsequent parameters of the function must also be optional.

Yes, that's correct. I'm assuming you had a specific application to
either the thread or my message when you wrote this -- could you
please elaborate?

0 new messages