Some refactoring to reduce code duplication when checking HRESULT values.