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

Problem in optional and defaultvalue MIDL attributes

147 views
Skip to first unread message

Kumar.V.K@discussions.microsoft.com Vinoth Kumar.V.K

unread,
Dec 5, 2004, 1:23:02 AM12/5/04
to
Hi

I have a problem while using the optional and defaultvalue MIDL attributes
in a method declaration.

Here is my sample MIDL method declaration

HRESULT Add ( [in] int a, [in] int b, [in, optional, defaultvalue(10)] int c)

I have written a COM client by importing the typelibrary of the above IDL
defition

but while calling the Add method from the client it ask me to give all the
three parameters eventhough i declared the third parameter as optional value

but if i use the ".h" and "_i.c" files the compiler accepting two arguments.

May i know why this discrepencey?

can i get the optional attribute feature whil imporing the tlb file also?
please suggest me a way to use optional parameters by imporing the tlbs in
the client side code

this same code is working fin in VB.

Waiting for your response

with regards

vinoth .v.k


Kim Gräsman

unread,
Dec 5, 2004, 3:42:57 AM12/5/04
to
Hi Vinoth,

> HRESULT Add ( [in] int a, [in] int b, [in, optional, defaultvalue(10)] int c)
>
> I have written a COM client by importing the typelibrary of the above IDL
> defition
>
> but while calling the Add method from the client it ask me to give all the
> three parameters eventhough i declared the third parameter as optional value

#import doesn't always carry over the optional and defaultvalue attributes. I believe it only does it for VARIANTs, though I haven't looked at it that carefully.

One option that might work for you is to make the last value a VARIANT instead, and then in your implementation check:

STDMETHODIMP CMyImpl::Add(..., VARIANT c)
{
HRESULT hr;

int actualC = 10;
if (c.vt != VT_ERROR)
{
// Specify better flags if you care about locale conversions, etc.
hr = VariantChangeType(&c, &c, 0, VT_I4);
if (FAILED(hr)) return hr;

actualC = c.lVal;
}

// Work with actualC from here on
}

That should work with both VB and C++ clients.

--
Best regards,
Kim Gräsman

Alexander Nickolov

unread,
Dec 6, 2004, 1:34:26 PM12/6/04
to
[optional] and [defaultvalue()] don't make sense together.
The argument is either optional (no value at all), or has a
default value supplied by the caller (if the program source
didn't provide one). Default values can only be applied
to scalar arguments. [optional] can only be applied to
VARIANT arguments. Even though both look the same,
the mechanics underneath are completely different.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Vinoth Kumar.V.K" <Vinoth Kuma...@discussions.microsoft.com> wrote in
message news:C17E4C2B-CF46-4F06...@microsoft.com...

kartiklunkad

unread,
Dec 20, 2011, 6:21:38 AM12/20/11
to
Alexander Nickolov wrote on 12/06/2004 13:34 ET :
> [optional] and [defaultvalue()] don't make sense together.
> The argument is either optional (no value at all), or has a
> default value supplied by the caller (if the program source
> didn't provide one). Default values can only be applied
> to scalar arguments. [optional] can only be applied to
> VARIANT arguments. Even though both look the same,
> the mechanics underneath are completely different.
>
> Alexander Nickolov
> Microsoft MVP [VC], MCSD
> email:
> MVP VC FAQ: http://www.mvps.org/vcfaq
>
> "Vinoth Kumar.V.K" <Vinoth
> wrote in
> message news:
>> Hi
>>
>> I have a problem while using the optional and defaultvalue MIDL attributes
>> in a method declaration.
>>
>> Here is my sample MIDL method declaration
>>
>> HRESULT Add ( [in] int a, [in] int b, [in, optional, defaultvalue(10)] int
>> c)
>>
>> I have written a COM client by importing the typelibrary of the above IDL
>> defition
>>
>> but while calling the Add method from the client it ask me to give all the
>> three parameters eventhough i declared the third parameter as optional
>> value
>>
>> but if i use the ".h" and "_i.c" files the compiler
>> accepting two
>> arguments.
>>
>> May i know why this discrepencey?
>>
>> can i get the optional attribute feature whil imporing the tlb file also?
>> please suggest me a way to use optional parameters by imporing the tlbs in
>> the client side code
>>
>> this same code is working fin in VB.
>>
>> Waiting for your response
>>
>> with regards
>>
>> vinoth .v.k
>>
>>
>>
>>
>>
>>
>
As you have said [optional] and [defaultvalue()] don't make sense together.
Can
you provide an example of how either are implemented in MIDL function
declaration and then the function implementation in C++. Or if you could lead
me
to a place where I could find this as I am unable to find it on Google .
Thanks
Alot.

Regards,
Kartik Lunkad
0 new messages