By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,367 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,367 IT Pros & Developers. It's quick & easy.

Interop: How to hide .NET return type from COM?

P: n/a
Consider a C# function that logs an error string and returns a new
Exception object:
System.Exception LogError(string msg)
{
...
return new Exception(msg);
}

In my program, this method is exposed to both .NET and COM clients.
For C++ COM client, the default translation of this method is as
follows:
virtual HRESULT __stdcall LogError (
/*[in]*/ BSTR msg,
/*[out,retval]*/ struct _Exception * * pRetVal ) = 0;

The problem that I'm having is that my COM client has no use for .NET
Exception being returned to it. Is it possible (via some attribute?)
to make the compiler discard the Exception return type, so that the
generated function signature is like this (ie. discards the Exception
return type):

virtual HRESULT __stdcall LogError (
/*[in]*/ BSTR msg ) = 0;

or in other words, corresponds to C# signature of "void
LogError(string)"? I've looked at [return: MarshalAs(...)] attribute
but this doesn't let me "void"/discard the Exception return.
Many Thanks.
-K

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
P.S. Any other way would be OK, as long as from C++ the function can
be called as
HRESULT LogError(BSTR msg)
and from C# as
Exception LogError(string msg)

Obviously, I could just rename/implement the function in C# as
"LogErrorCOM" (so now there are 2 methods: LogError exposed to .NET
clients and LogErrorCOM exposed to COM clients) but I dislike this
solution... there should certainly be a way to make a return value of a
method irrelevant to COM clients (not even bother marshaling it).

Thanks.

Nov 17 '05 #2

P: n/a
You can declare the managed implementation to return void and throw the exception. I assume the Marshaller will return the HResult
of the exception instead of the exception instance (you can set the Hresult before throwing it) when called from an unmanged
environment.

Managed clients can use try...catch around calls that may or may not invoke your method, although this is a sloppy use of
Exceptions.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"breathwell" <kk@efficientsoftware.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
P.S. Any other way would be OK, as long as from C++ the function can
be called as
HRESULT LogError(BSTR msg)
and from C# as
Exception LogError(string msg)

Obviously, I could just rename/implement the function in C# as
"LogErrorCOM" (so now there are 2 methods: LogError exposed to .NET
clients and LogErrorCOM exposed to COM clients) but I dislike this
solution... there should certainly be a way to make a return value of a
method irrelevant to COM clients (not even bother marshaling it).

Thanks.

Nov 17 '05 #3

P: n/a
Hi Dave, thanks.

It's not so much that I'm trying to convert Exception to HRESULT as the
fact that my COM client is not interested in the return value that
comes back from a .NET method. By default the return value of .NET
function gets converted into an [out] parameter -- this is something
that I wish to avoid.

Nov 17 '05 #4

P: n/a
Yea, I understand about the "out" parameter and how you'd rather not have it in your unmanaged call, that's why I'm suggessting to
return void in the managed implementation. If you then throw the exception, your .NET clients can still obtain the value by using a
Try...Catch statement, which I'm assuming is required since you are "returning" a value in the first place. The fact that the
return value is of the Type "Exception" allows for the solution I've given to you above to work.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"breathwell" <kk@efficientsoftware.com> wrote in message news:11**********************@l41g2000cwc.googlegr oups.com...
Hi Dave, thanks.

It's not so much that I'm trying to convert Exception to HRESULT as the
fact that my COM client is not interested in the return value that
comes back from a .NET method. By default the return value of .NET
function gets converted into an [out] parameter -- this is something
that I wish to avoid.

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.