Exception handling COM to .NET



I have a C# application talking to a COM dll through Interop services.
When there is an exception in the COM it gives me back a HRESULT that I
can trap in my catch block
.. Is there anyway I can avoid going to the catch block and handle the
returning HRESULT before that ? The reason being
**"COM methods may occasionally change the data inside by-reference
parameters before returning a failure HRESULT. When such a method is
called from managed code, however, the updated values of any
by-reference parameters are not copied back to the caller before the
..NET exception is thrown ".**
I need to preserve the updated values of the call even though an
exception has occurred.



Nicholas Paldino [.NET/C# MVP]


In your interface definition that you use to access COM, you can add the
PreserveSig attribute to the method. You then have to change the method
definition so that it returns an int (which represents the HRESULT) and the
"return" value is the last parameter in the method. Basically, you would
define the method exactly the way you would in IDL (well, mapped to .NET at
least, but the return value and parameter lists are the same).

You can then make the call and check the return value yourself.

Hope this helps.

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