/// <summary> /// The method calls RoOriginateLanguageException. The method has all the logic in try, catch block to ensure that none of the exception helpers /// throw exception themselves. /// </summary> /// <param name="ex"></param> /// <returns></returns> private static bool OriginateLanguageException(Exception ex) { IntPtr pUnk = IntPtr.Zero; HSTRING errorMsg = default(HSTRING); try { pUnk = McgMarshal.ObjectToComInterface(ex, InternalTypes.IUnknown); if (pUnk != IntPtr.Zero) { RuntimeAugments.GenerateExceptionInformationForDump(ex, pUnk); errorMsg = McgMarshal.StringToHString(ex.Message); return(ExternalInterop.RoOriginateLanguageException(ex.HResult, errorMsg, pUnk) >= 0); } } catch (Exception) { // We can't do anything here and hence simply swallow the exception } finally { McgMarshal.ComSafeRelease(pUnk); if (errorMsg.handle != IntPtr.Zero) { ExternalInterop.WindowsDeleteString(errorMsg.handle.ToPointer()); } } return(false); }
internal static Exception AttachRestrictedErrorInfo(Exception e) { // If there is no exception, then the restricted error info doesn't apply to it if (e != null) { System.IntPtr pRestrictedErrorInfo = IntPtr.Zero; try { // Get the restricted error info for this thread and see if it may correlate to the current // exception object. Note that in general the thread's IRestrictedErrorInfo is not meant for // exceptions that are marshaled Windows.Foundation.HResults and instead are intended for // HRESULT ABI return values. However, in many cases async APIs will set the thread's restricted // error info as a convention in order to provide extended debugging information for the ErrorCode // property. if (TryGetRestrictedErrorInfo(out pRestrictedErrorInfo)) { string description; string restrictedDescription; string capabilitySid; int restrictedErrorInfoHResult; if (RestrictedErrorInfoHelper.GetErrorDetails( pRestrictedErrorInfo, out description, out restrictedErrorInfoHResult, out restrictedDescription, out capabilitySid) && (e.HResult == restrictedErrorInfoHResult)) { // Since this is a special case where by convention there may be a correlation, there is not a // guarantee that the restricted error info does belong to the async error code. In order to // reduce the risk that we associate incorrect information with the exception object, we need // to apply a heuristic where we attempt to match the current exception's HRESULT with the // HRESULT the IRestrictedErrorInfo belongs to. If it is a match we will assume association // for the IAsyncInfo case. string errorReference; RestrictedErrorInfoHelper.GetReference(pRestrictedErrorInfo, out errorReference); object restrictedErrorInfo = McgMarshal.ComInterfaceToObject(pRestrictedErrorInfo, InternalTypes.IRestrictedErrorInfo); InteropExtensions.AddExceptionDataForRestrictedErrorInfo( e, restrictedDescription, errorReference, capabilitySid, restrictedErrorInfo); } } } catch (Exception) { // If we can't get the restricted error info, then proceed as if it isn't associated with this // error. } finally { McgMarshal.ComSafeRelease(pRestrictedErrorInfo); } } return(e); }
#pragma warning restore 649, 169 /// <summary> /// This method gets the mapping hr for the exception. and also does the right thing to propogate the hr correctly to the native layer. /// /// We check if the exception is a pure managed exception or an exception created from an hr that entered the system from native. /// a. If it is a pure managed exception we create an IUnknown ptr from the exception and RoOriginateLanguageException on it. /// This helps us to preserve our managed exception and throw the same exception in case this exception roundtrips and hence preserve the call stack. /// Since the API RoOriginateLanguageException is available only on windows blue, we can't do the same in win8. In desktop CLR we use the non-modern SDK API /// GetErroInfo\SetErrorInfo combination to preserve managed exception but unfortunately we can't do this in .NET Native and hence we only our able to preserve the exception message and /// type and end up getting a rough stacktrace PS - Even this behavior in win8 is possible only in debug mode as RoSetErrorReportingFlags is set to UseSetErrorInfo only in debug mode. /// /// b. In case the exception is created due to an hr that entered managed world via native call, we will have restrictederrorInfo associated with it. In this case /// we do not RoOriginateLanguageException\RoOriginateError and rather preserve the exception stack trace by simply calling the SetRestrictedErrorInfo. /// /// c. PS - Due to the use of modern SDK we have no way to round trip exceptions in classicCOM scenarios any more. /// This is because we can't use SetErrorInfo\GetErrorInfo APIs at all. Unfortunately we have no workaround for this even in windowsBlue! /// With the use of IRestrictedErrorInfo has some disadvantages as we lose other info available with IErrorInfo in terms of HelpFile etc. /// /// d. This class puts all the logic in try, catch block to ensure that none of the exception helpers. /// throw exception themselves. /// </summary> /// <param name="ex"></param> /// <param name="isWinRTScenario"></param> /// <returns></returns> internal static int GetHRForExceptionWithErrorPropogationNoThrow(Exception ex, bool isWinRTScenario) { int hr = ex.HResult; if (hr == Interop.COM.COR_E_OBJECTDISPOSED && isWinRTScenario) { // Since ObjectDisposedException is projected to RO_E_CLOSED in WINRT we make sure to use the correct hr while updating the CRuntimeError object of Windows. hr = Interop.COM.RO_E_CLOSED; } try { // Check whether the exception has an associated RestrictedErrorInfo associated with it. if (isWinRTScenario) { IntPtr pRestrictedErrorInfo; object restrictedErrorInfo; if (InteropExtensions.TryGetRestrictedErrorObject(ex, out restrictedErrorInfo) && restrictedErrorInfo != null) { // We have the restricted errorInfo associated with this object and hence this exception was created by an hr entering managed through native. pRestrictedErrorInfo = McgMarshal.ObjectToComInterface(restrictedErrorInfo, InternalTypes.IRestrictedErrorInfo); if (pRestrictedErrorInfo != IntPtr.Zero) { // We simply call SetRestrictedErrorInfo since we do not want to originate the exception again. ExternalInterop.SetRestrictedErrorInfo(pRestrictedErrorInfo); McgMarshal.ComSafeRelease(pRestrictedErrorInfo); } } else { // we are in windows blue and hence we can preserve our exception so that we can reuse this exception in case it comes back and provide richer exception support. OriginateLanguageException(ex); } } else { // We are either pre WinBlue or in classicCOM scenario and hence we can only RoOriginateError at this point. // Desktop CLR uses SetErrorInfo and preserves the exception object which helps us give the same support as winBlue. // Since .NET Native can only use modern SDK we have a compatibility break here by only preserving the restrictederrorMsg and exception type but the stack trace will be incorrect. // Also RoOriginateError works only under the debugger since RoSetErrorReportingFlags is set to RO_ERROR_REPORTING_USESETERRORINFO. // If we are not under the debugger we can't set this API since it is not part of the modernSDK and hence this will not work // and will result in different behavior than the desktop. HSTRING errorMsg = McgMarshal.StringToHString(ex.Message); ExternalInterop.RoOriginateError(ex.HResult, errorMsg); ExternalInterop.WindowsDeleteString(errorMsg.handle.ToPointer()); } } catch (Exception) { // We can't throw an exception here and hence simply swallow it. } return(hr); }
#pragma warning restore 649, 169 /// <summary> /// This method gets called every time the WeakReference or WeakReference'1 set the Target. /// We can have 4 possible combination here. /// a. Target is a GC object and it is either getting set for the first time or previous object is also GC object. /// In this case we do not need to do anything. /// /// b. Target is a GC object and previous target was __ComObject /// i. We remove the element from ConditionalWeakTable. /// ii. When the GC collects this ComWeakReference the finalizer will ensure that the native object is released. /// /// c. Target is a __ComObject and the previous target was null or GC object. /// We simply add the new target to the dictionary. /// /// d. Target is a __COmObject and the previous object was __COmObject. /// i. We first remove the element from the ConditionalWeakTable. /// ii. When the GC collects the previous ComWeakReference the finalizer will ensure that the native object is released. /// iii. We add the new ComWeakReference to the conditionalWeakTable. /// </summary> /// <param name="weakReference"></param> /// <param name="target"></param> public static unsafe void SetTarget(object weakReference, object target) { Contract.Assert(weakReference != null); // Check if this weakReference is already associated with a native target. ComWeakReference pOldComWeakReference; if (s_COMWeakReferenceTable.TryGetValue(weakReference, out pOldComWeakReference)) { // Remove the previous target. // We do not have to release the native ComWeakReference since it will be done as part of the finalizer. s_COMWeakReferenceTable.Remove(weakReference); } // Now check whether the current target is __ComObject. // However, we don't want to pass the QI to a managed object deriving from native object - we // would end up passing the QI to back the CCW and end up stack overflow __ComObject comObject = target as __ComObject; if (comObject != null && !comObject.ExtendsComObject) { // Get the IWeakReferenceSource for the given object IntPtr pWeakRefSource = McgMarshal.ObjectToComInterface(comObject, McgModuleManager.IWeakReferenceSource); if (pWeakRefSource != IntPtr.Zero) { IntPtr pWeakRef = IntPtr.Zero; try { // Now that we have the IWeakReferenceSource , we need to call the GetWeakReference method to get the corresponding IWeakReference __com_IWeakReferenceSource *pComWeakRefSource = (__com_IWeakReferenceSource *)pWeakRefSource; int result = CalliIntrinsics.StdCall <int>( pComWeakRefSource->pVtable->pfnGetWeakReference, pWeakRefSource, out pWeakRef); if (result >= 0 && pWeakRef != IntPtr.Zero) { // Since we have already checked s_COMWeakReferenceTable for the weak reference, we can simply add the entry w/o checking. // PS - We do not release the pWeakRef as it should be alive until the ComWeakReference is alive. s_COMWeakReferenceTable.Add(weakReference, new ComWeakReference(ref pWeakRef)); } } finally { McgMarshal.ComSafeRelease(pWeakRef); McgMarshal.ComRelease(pWeakRefSource); } } } }
internal static bool ReportUnhandledError(Exception e) { System.IntPtr pRestrictedErrorInfo = IntPtr.Zero; if (e != null) { try { #if ENABLE_WINRT // Only report to the WinRT global exception handler in modern apps WinRTInteropCallbacks callbacks = WinRTInterop.Callbacks; if (callbacks == null || !callbacks.IsAppxModel()) { return(false); } // Get the IUnknown for the current exception and originate it as a langauge error in order to have // Windows generate an IRestrictedErrorInfo corresponding to the exception object. We can then // notify the global error handler that this IRestrictedErrorInfo instance represents an exception that // went unhandled in managed code. if (OriginateLanguageException(e) && TryGetRestrictedErrorInfo(out pRestrictedErrorInfo)) { return(ExternalInterop.RoReportUnhandledError(pRestrictedErrorInfo) >= 0); } #else return(false); #endif // ENABLE_WINRT } catch (Exception) { // We can't give an exception in this code, so we simply swallow the exception here. } finally { McgMarshal.ComSafeRelease(pRestrictedErrorInfo); } } // If we have got here, then some step of the pInvoke failed, which means the GEH was not invoked return(false); }
/// <summary> /// This method returns a new Exception object given the HR value. /// /// 1. We check whether we have our own LanguageException associated with this hr. If so we simply use it since it helps preserve the stacktrace, message and type. /// This is done using GetLanguageException API on ILanguageExceptionErrorInfo from IRestrictedErrorInfo. Since ILanguageExceptionErrorInfo is available only on Windows Blue /// we can only do this WindowsBlue and above. In desktop CLR we could use GetErroInfo and check whether we have our IErroInfo and retrieve our own exception. /// For Win8 in .NET Native we simply create the exception using the RestrictedErrorInfo and hence only able to give the exception with restrictedErrorMsg. /// 2. In case we do not have the languageException we simply check RestrictedErrorInfo for errorMsg and create an exception using /// <errorMsg>\r\n<restrictedErrorMsg>. This is done for only windows blue. To be backward compatible we only use errorMsg for creating exception in win8. /// 3. PS - This class puts all the logic in try, catch block to ensure that none of the exception helpers /// throw exception themselves. /// </summary> /// <param name="hr"></param> /// <param name="isWinRTScenario"></param> internal static Exception GetExceptionForHRInternalNoThrow(int hr, bool isWinRTScenario, bool isClassicCOM) { Exception ex; IntPtr pRestrictedErrorInfo = IntPtr.Zero; try { if (TryGetRestrictedErrorInfo(out pRestrictedErrorInfo)) { // This is to check whether we need to give post win8 behavior or not. if (isWinRTScenario) { // Check whether the given IRestrictedErrorInfo object supports ILanguageExceptionErrorInfo IntPtr pLanguageExceptionErrorInfo = McgMarshal.ComQueryInterfaceNoThrow(pRestrictedErrorInfo, ref Interop.COM.IID_ILanguageExceptionErrorInfo); if (pLanguageExceptionErrorInfo != IntPtr.Zero) { // We have an LanguageExceptionErrorInfo. IntPtr pUnk; __com_ILanguageExceptionErrorInfo *pComLanguageExceptionErrorInfo = (__com_ILanguageExceptionErrorInfo *)pLanguageExceptionErrorInfo; int result = CalliIntrinsics.StdCall <int>(pComLanguageExceptionErrorInfo->pVtable->pfnGetLanguageException, pLanguageExceptionErrorInfo, out pUnk); McgMarshal.ComSafeRelease(pLanguageExceptionErrorInfo); if (result >= 0 && pUnk != IntPtr.Zero) { try { // Check whether the given pUnk is a managed exception. ComCallableObject ccw; if (ComCallableObject.TryGetCCW(pUnk, out ccw)) { return(ccw.TargetObject as Exception); } } finally { McgMarshal.ComSafeRelease(pUnk); } } } } String message = null, errorInfoReference = null; string errMsg, errCapSid, resErrMsg; int errHr; object restrictedErrorInfo = null; bool hasErrorInfo = false; if (RestrictedErrorInfoHelper.GetErrorDetails(pRestrictedErrorInfo, out errMsg, out errHr, out resErrMsg, out errCapSid) && errHr == hr) { // RestrictedErrorInfo details can be used since the pRestrictedErrorInfo has the same hr value as the hr returned by the native code. // We are in windows blue or above and hence the exceptionMsg is errMsg + "\r\n" + resErrMsg message = String.IsNullOrEmpty(resErrMsg) ? errMsg : errMsg + "\r\n" + resErrMsg; RestrictedErrorInfoHelper.GetReference(pRestrictedErrorInfo, out errorInfoReference); restrictedErrorInfo = McgMarshal.ComInterfaceToObject(pRestrictedErrorInfo, InternalTypes.IRestrictedErrorInfo); hasErrorInfo = true; } if (hr == Interop.COM.RO_E_CLOSED && isWinRTScenario) { hr = Interop.COM.COR_E_OBJECTDISPOSED; } // Now we simply need to set the description and the resDescription by adding an internal method. ex = GetMappingExceptionForHR(hr, message, isClassicCOM, hasErrorInfo); if (restrictedErrorInfo != null) { InteropExtensions.AddExceptionDataForRestrictedErrorInfo(ex, resErrMsg, errorInfoReference, errCapSid, restrictedErrorInfo); } return(ex); } } catch (Exception) { // We can't do any thing here and hence we swallow the exception and get the corresponding hr. } finally { McgMarshal.ComSafeRelease(pRestrictedErrorInfo); } // We could not find any restrictedErrorInfo associated with this object and hence we simply use the hr to create the exception. return(GetMappingExceptionForHR(hr, null, isClassicCOM, hasErrorInfo: false)); }
/// <summary> /// Returns the existing RCW or create a new RCW from the COM interface pointer /// NOTE: This does unboxing unless CreateComObjectFlags.SkipTypeResolutionAndUnboxing is specified /// </summary> /// <param name="expectedContext"> /// The current context of this thread. If it is passed and is not Default, we'll check whether the /// returned RCW from cache matches this expected context. If it is not a match (from a different /// context, and is not free threaded), we'll go ahead ignoring the cached entry, and create a new /// RCW instead - which will always end up in the current context /// We'll skip the check if current == ContextCookie.Default. /// </param> internal static object ComInterfaceToComObjectInternal( IntPtr pComItf, IntPtr pComIdentityIUnknown, RuntimeTypeHandle interfaceType, RuntimeTypeHandle classTypeInSignature, ContextCookie expectedContext, CreateComObjectFlags flags ) { string className; object obj = ComInterfaceToComObjectInternal_NoCache( pComItf, pComIdentityIUnknown, interfaceType, classTypeInSignature, expectedContext, flags, out className ); // // The assumption here is that if the classInfoInSignature is null and interfaceTypeInfo // is either IUnknow and IInspectable we need to try unboxing. // bool doUnboxingCheck = (flags & CreateComObjectFlags.SkipTypeResolutionAndUnboxing) == 0 && obj != null && classTypeInSignature.IsNull() && (interfaceType.Equals(InternalTypes.IUnknown) || interfaceType.IsIInspectable()); if (doUnboxingCheck) { // // Try unboxing // Even though this might just be a IUnknown * from the signature, we still attempt to unbox // if it implements IInspectable // // @TODO - We might need to optimize this by pre-checking the names to see if they // potentially represents a boxed type, but for now let's keep it simple and I also don't // want to replicate the knowledge here // @TODO2- We probably should skip the creating the COM object in the first place. // // NOTE: the RCW here could be a cached one (for a brief time if GC doesn't kick in. as there // is nothing to hold the RCW alive for IReference<T> RCWs), so this could save us a RCW // creation cost potentially. Desktop CLR doesn't do this. But we also paying for unnecessary // cache management cost, and it is difficult to say which way is better without proper // measuring // object unboxedObj = McgMarshal.UnboxIfBoxed(obj, className); if (unboxedObj != null) { return(unboxedObj); } } // // In order for variance to work, we save the incoming interface pointer as specified in the // signature into the cache, so that we know this RCW does support this interface and variance // can take advantage of that later // NOTE: In some cases, native might pass a WinRT object as a 'compatible' interface, for example, // pass IVector<IFoo> as IVector<Object> because they are 'compatible', but QI for IVector<object> // won't succeed. In this case, we'll just believe it implements IVector<Object> as in the // signature while the underlying interface pointer is actually IVector<IFoo> // __ComObject comObject = obj as __ComObject; if (comObject != null) { McgMarshal.ComAddRef(pComItf); try { comObject.InsertIntoCache(interfaceType, ContextCookie.Current, ref pComItf, true); } finally { // // Only release when a exception is thrown or we didn't 'swallow' the ref count by // inserting it into the cache // McgMarshal.ComSafeRelease(pComItf); } } return(obj); }