private static Exception GetExceptionForHR(int hr, bool useGlobalErrorState, out bool restoredExceptionFromGlobalState) { restoredExceptionFromGlobalState = false; if (hr >= 0) { return(null); } ObjectReference <ABI.WinRT.Interop.IErrorInfo.Vftbl> iErrorInfo = null; IObjectReference restrictedErrorInfoToSave = null; Exception ex; string description = null; string restrictedError = null; string restrictedErrorReference = null; string restrictedCapabilitySid = null; bool hasOtherLanguageException = false; if (useGlobalErrorState && getRestrictedErrorInfo != null) { Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(out IntPtr restrictedErrorInfoPtr)); if (restrictedErrorInfoPtr != IntPtr.Zero) { IObjectReference restrictedErrorInfoRef = ObjectReference <ABI.WinRT.Interop.IRestrictedErrorInfo.Vftbl> .Attach(ref restrictedErrorInfoPtr); restrictedErrorInfoToSave = restrictedErrorInfoRef.As <ABI.WinRT.Interop.IRestrictedErrorInfo.Vftbl>(); ABI.WinRT.Interop.IRestrictedErrorInfo restrictedErrorInfo = new ABI.WinRT.Interop.IRestrictedErrorInfo(restrictedErrorInfoRef); restrictedErrorInfo.GetErrorDetails(out description, out int hrLocal, out restrictedError, out restrictedCapabilitySid); restrictedErrorReference = restrictedErrorInfo.GetReference(); if (restrictedErrorInfoRef.TryAs <ABI.WinRT.Interop.ILanguageExceptionErrorInfo.Vftbl>(out var languageErrorInfoRef) >= 0) { ILanguageExceptionErrorInfo languageErrorInfo = new ABI.WinRT.Interop.ILanguageExceptionErrorInfo(languageErrorInfoRef); using IObjectReference languageException = languageErrorInfo.GetLanguageException(); if (languageException is object) { if (languageException.IsReferenceToManagedObject) { ex = ComWrappersSupport.FindObject <Exception>(languageException.ThisPtr); if (GetHRForException(ex) == hr) { restoredExceptionFromGlobalState = true; return(ex); } } else { hasOtherLanguageException = true; } } } else { if (hr == hrLocal) { restrictedErrorInfoRef.TryAs <ABI.WinRT.Interop.IErrorInfo.Vftbl>(out iErrorInfo); } } } }
public SingleInterfaceOptimizedObject(Type type, IObjectReference objRef) { _type = type; Type helperType = type.FindHelperType(); var vftblType = helperType.FindVftblType(); if (vftblType is null) { _obj = objRef.As <IUnknownVftbl>(GuidGenerator.GetIID(helperType)); } else { _obj = (IObjectReference)typeof(IObjectReference).GetMethod("As", Type.EmptyTypes).MakeGenericMethod(vftblType).Invoke(objRef, null); } }