How to call a COM DLL from C #, handling HRESULT errors?
I am creating a COM interface that C # will use, however I am wondering how I can check for errors to handle exceptions at the end of C #. For now, I just return HRESTULT or bool for most of the methods and then do Marshal.ThrowExceptionForHR, but some things might go wrong in some of these methods, and returning E_FAIL just doesn't shortcut it.
What can I do to return additional information? Can I make my own HRESULT?
a source to share
I have never done this, but in theory you should do it supporting IErrorInfo . Marshal.GetExceptionForHR()
has an overload that takes an IErrorInfo pointer.
By the way, you don't need to manually call Marshal.ThrowExceptionForHR()
- if your method returns HRESULT parameters [out]
for the return value too , you can declare it in .NET as a return value and set PreserveSig = false , and the CLR will automatically throw an exception if the HRESULT indicates a failure.
a source to share