473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Interop and not passing optional out params

Hi all,

Im attempting to use a COM class in C# via .NET interop. The class has two
modes - synhrounous and asynchronous. The mode is determined by the use (or
not) of an optional out parameter:

COMClass test = new COMClass();

object results = null;

test.DoWork(arg1, arg2, out results); //synchronous call - results are in
results after execution.

test.AsyncReturn += eventhandler;

test.DoWork(arg1, arg2, out null); //asynchronous call - fires
test.AsyncReturn.

Im sure you can see the problem here - the third out argument cannot be
null. Neither can Type.Missing or Reflection.Missing.Value - these are both
readonly fields. Omitting the out results in a type error. If I try:

object results = Type.Missing;

test.DoWork(arg1, arg2, out results);

then the call is synchonous, no calls are made to the eventhandler and there
is data in results after the call. Is there any way of specifying an
optional out parameter as not being there?

Thanks in advance!

Spammy
Jul 21 '05 #1
6 2120
spammy <me@privacy.net> wrote:
Im attempting to use a COM class in C# via .NET interop. The class has two
modes - synhrounous and asynchronous. The mode is determined by the use (or
not) of an optional out parameter:


Hmm. This is odd - one of the things about an "out" parameter is that
it doesn't need to be definitely assigned before use - in other words,
the initial vaule shouldn't matter.

Perhaps it should be a ref parameter instead of out?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
spammy <me@privacy.net> wrote:
Im attempting to use a COM class in C# via .NET interop. The class has two modes - synhrounous and asynchronous. The mode is determined by the use (or not) of an optional out parameter:


Hmm. This is odd - one of the things about an "out" parameter is that
it doesn't need to be definitely assigned before use - in other words,
the initial vaule shouldn't matter.

Perhaps it should be a ref parameter instead of out?


The direction itself was decided when I added the COM as a reference. To be
clear, I can pass an unintialised object to the function too:

object results;
test.DoWork(arg1, arg2, out results); //synchronous call

and it would work. Basically the old value of results (even if that happens
to be a reference to Missing) are overwritten by DoWork. I need to
explicitly leave it out rather than leave a placeholder - Can I possibly add
a new method to the COM skeleton interop wrapper leaving out the optional
parameter?

Alternatively I read that I actually need the runtime to pass a variant of
type VT_ERROR, set to DISP_E_PARAMNOTFOUND. Is it possible for me to do this
manually?

Spammy

Jul 21 '05 #3
spammy <me@privacy.net> wrote:
The direction itself was decided when I added the COM as a reference. To be
clear, I can pass an unintialised object to the function too:

object results;
test.DoWork(arg1, arg2, out results); //synchronous call
Indeed.
and it would work. Basically the old value of results (even if that happens
to be a reference to Missing) are overwritten by DoWork.
Yes - they have to be.
I need to explicitly leave it out rather than leave a placeholder - Can I
possibly add a new method to the COM skeleton interop wrapper leaving out
the optional parameter?
I don't know, to be honest.
Alternatively I read that I actually need the runtime to pass a variant of
type VT_ERROR, set to DISP_E_PARAMNOTFOUND. Is it possible for me to do this
manually?


If you're passing information in which ends up being read, it shouldn't
be an out parameter.

I suggest you make it *not* be an out parameter, and *not* an optional
parameter either.

Out parameters are for values which aren't read in the called method -
they're only written. Optional parameters just aren't supported in C#.
How those two facets interact with COM, I don't know (and you should
possibly ask on the .interop group) but those are the two important
things from the C# side.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Alternatively I read that I actually need the runtime to pass a variant of type VT_ERROR, set to DISP_E_PARAMNOTFOUND. Is it possible for me to do this manually?
If you're passing information in which ends up being read, it shouldn't
be an out parameter.


Im not sure whether it is being read or not being read - I suspect its the
presence of the variable itself along with it being an out parameter that is
tripping up the marshalling somehow, and not providing it as a variant
VT_ERROR etc.
I suggest you make it *not* be an out parameter, and *not* an optional
parameter either.
Again, its not me thats doing it. Although I am currently looking at the
disassembled interop assembly to see if i can override the automated out
param and change it to an in one.
Out parameters are for values which aren't read in the called method -
they're only written. Optional parameters just aren't supported in C#.
How those two facets interact with COM, I don't know (and you should
possibly ask on the .interop group) but those are the two important
things from the C# side.


Interop group? Doh, Im on my way, thanks!

Spammy

Jul 21 '05 #5
Hi Jon,

Is it not better to give the advice direct to ask this question in the
Interop group when you are not sure that you can answer this question?

Cor
Jul 21 '05 #6
Cor Ligthert <no**********@planet.nl> wrote:
Is it not better to give the advice direct to ask this question in the
Interop group when you are not sure that you can answer this question?


I've answered the C# side as well as I can, and already suggested that
he asks in the .interop group for more help.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
4
by: Yong Jiang | last post by:
I am trying to convert some vb code calling COM oboject methods to C#. Example shown as follows Dim Stream As New ADODB.Stream Call Stream.Open() Stream.Open has three optional parameters. ...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
5
by: Imran Aziz | last post by:
Hello All, I am new to C# , how can I delare parameters to a function as optional, and also how are default values assigned ? consider the function public String myFunc(String thisisOptional,...
10
by: Sebastian Santacroce | last post by:
Hi, If I want to pass a form (forms I have created) to a function what would I set the declaration as for example Dim p as existingForm OpenForm (p, existingForm)
6
by: spammy | last post by:
Hi all, Im attempting to use a COM class in C# via .NET interop. The class has two modes - synhrounous and asynchronous. The mode is determined by the use (or not) of an optional out parameter:...
2
by: Ross | last post by:
Hi folks This is just a general question. Suppose I have a SP that has several params and this includes one optional param. If I have all the params as args for the WS, and suppose for a...
0
by: Udi | last post by:
Hi all, I'm having difficulties returning a buffer allocated on a callback called from a native dll to .NET assembly. (See pseudo code below in "Foo" func): The managed assembly (the called back...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.