473,757 Members | 6,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(arg 1, arg2, out results); //synchronous call - results are in
results after execution.

test.AsyncRetur n += eventhandler;

test.DoWork(arg 1, arg2, out null); //asynchronous call - fires
test.AsyncRetur n.

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

object results = Type.Missing;

test.DoWork(arg 1, 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 2147
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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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(arg 1, 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_PARAMNOT FOUND. 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(arg 1, 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_PARAMNOT FOUND. 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Alternatively I read that I actually need the runtime to pass a variant of type VT_ERROR, set to DISP_E_PARAMNOT FOUND. 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**********@p lanet.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.co m>
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
2515
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. I've done this before with Modules, and saw that <gasp> they behave just like sealed classes with only static members. Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in...
4
3792
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. I am trying to convert it to C# as shown as follows ADODB.Stream Stream = null;
16
4061
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
5
7420
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, String OptWithDefaultValue) { }
10
4320
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
337
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: COMClass test = new COMClass(); object results = null;
2
1205
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 particular case there is no data in the calling app for the optional param in the SP (note that there are no optional params for the WS because of SOAP).
0
1651
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 function) needs to allocate the buffer and return it as an out param to the "C" dll. When invoking the callback from the C code, passing a valid pointer with sizeParam=0 cause the application to crash - I can't even enter the callback scope....
0
9489
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.