Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create COMException from HRESULT

Tags:

c#

com

How do one create a COMException given a HRESULT?

I have P/Invoke:ed a win32-method which returns a HRESULT.
If it returns failure I wish to throw a COMException, with the standard error-text.
How should I do that?

  1. throw new COMException(null, hResult)

  2. COMException e = new COMException;
    e.HResult = hResult;
    throw e;

Or should I use some other method?

like image 861
leiflundgren Avatar asked Nov 01 '25 08:11

leiflundgren


1 Answers

You should use Marshal.GetExceptionForHR() if you only want to get the exception, or Marshal.ThrowExceptionForHR() if want to throw it too.

like image 176
svick Avatar answered Nov 02 '25 21:11

svick