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

B

breathwell

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
 
B

breathwell

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.
 
D

Dave

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.
 
B

breathwell

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.
 
D

Dave

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top