Search code examples
c#winapierror-handlingpinvoke

ThrowExceptionForHR + GetHRForLastWin32Error vs Win32Exception


Are these calls equivalent?

  • Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
  • throw new Win32Exception();

Solution

  • They are not equivalent.

    • Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()) throws a COM exception. This could be System.Runtime.InteropServices.COMException or an exception appropriate to the Win32 error code.
    • throw new Win32Exception() throws a Win32 exception, System.ComponentModel.Win32Exception.