PC Review


Reply
Thread Tools Rate Thread

C#, COM Interop & Default parameters

 
 
CMan
Guest
Posts: n/a
 
      26th May 2004
Hi All,

I am trying to use a COM component that has methods with default parameters.
How can I call these methods from C#. Not all default values are in the
documentation of the control.

Thanks

Colin


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      26th May 2004
Colin,

If the parameters have default values, then that means that they are
also optional parameters. In order to get the default value to be used, you
should pass the value returned from the static Value property on the Missing
class in the System.Reflection namespace.

The only problem that you will run into is when an interface is not
completely automation-compliant. VB and MIDL compilers allow interfaces to
have optional parameters that were of a type other than a variant. If your
COM components are like this, then you will have a problem, I think, because
the optional parameter will be exposed as that type, and trying to pass
Missing.Value will throw a InvalidCastException (I believe).

In this case, you might want to do some reflection (in the COM sense) on
the type library, getting the type, and then looking at the method
information. You should be able to get what you need through the ITypeLib
and ITypeInfo (their .NET counterparts being UCOMITypeLib and UCOMITypeInfo)
interfaces.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"CMan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All,
>
> I am trying to use a COM component that has methods with default

parameters.
> How can I call these methods from C#. Not all default values are in the
> documentation of the control.
>
> Thanks
>
> Colin
>
>



 
Reply With Quote
 
CMan
Guest
Posts: n/a
 
      26th May 2004
Thanks Nicholas

I'll have look at this. I may resort to VB for these few forms.

Colin


"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> Colin,
>
> If the parameters have default values, then that means that they are
> also optional parameters. In order to get the default value to be used,

you
> should pass the value returned from the static Value property on the

Missing
> class in the System.Reflection namespace.
>
> The only problem that you will run into is when an interface is not
> completely automation-compliant. VB and MIDL compilers allow interfaces

to
> have optional parameters that were of a type other than a variant. If

your
> COM components are like this, then you will have a problem, I think,

because
> the optional parameter will be exposed as that type, and trying to pass
> Missing.Value will throw a InvalidCastException (I believe).
>
> In this case, you might want to do some reflection (in the COM sense)

on
> the type library, getting the type, and then looking at the method
> information. You should be able to get what you need through the ITypeLib
> and ITypeInfo (their .NET counterparts being UCOMITypeLib and

UCOMITypeInfo)
> interfaces.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "CMan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi All,
> >
> > I am trying to use a COM component that has methods with default

> parameters.
> > How can I call these methods from C#. Not all default values are in the
> > documentation of the control.
> >
> > Thanks
> >
> > Colin
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default method on COM idl from C# interop Francisco Microsoft C# .NET 1 5th Nov 2007 04:04 PM
Proposal: Default Parameters/Named Parameters cody Microsoft C# .NET 8 14th Feb 2005 04:01 PM
COM Interop: Cascading default properties Chad Myers Microsoft C# .NET 0 17th Sep 2004 05:12 AM
What are parameters for Workbooks.Open() Using Interop.Excel? =?Utf-8?B?U2FyYWg=?= Microsoft Dot NET 1 27th Aug 2004 06:05 PM
C#, COM Interop & Default parameters CMan Microsoft C# .NET 2 26th May 2004 02:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:28 AM.