Example #1
0
        private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
        {
            WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;

            return(callbacks != null && callbacks.IsAppxModel() ?
                   callbacks.GetFolderPath(folder, option) :
                   null);
        }
Example #2
0
        /// <summary>
        /// Gets the default user culture from WinRT, if available.
        /// </summary>
        /// <remarks>
        /// This method may return null, if there is no default user culture or if WinRT isn't available.
        /// </remarks>
        private static CultureInfo GetUserDefaultCultureCacheOverride()
        {
            WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;

            if (callbacks != null && callbacks.IsAppxModel())
            {
                return((CultureInfo)callbacks.GetUserDefaultCulture());
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// This method notifies the System.private.Interop.dll to update the conditionalweaktable for weakreferenceInstance->target in case the target is __ComObject. This ensures that we have a means to
        /// go from the managed weak reference to the actual native object even though the managed counterpart might have been collected.
        /// </summary>
        /// <param name="target"></param>
        private void TrySetComTarget(object target)
        {
#if ENABLE_WINRT
            WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
            if (callbacks != null)
            {
                callbacks.SetCOMWeakReferenceTarget(this, target);
            }
            else
            {
                Debug.Assert(false, "WinRTInteropCallback is null");
            }
#endif // ENABLE_WINRT
        }
        // Used to report exceptions that *logically* go unhandled in the Fx code.  For example, an
        // exception that escapes from a ThreadPool workitem, or from a void-returning async method.
        public static void ReportUnhandledException(Exception exception)
        {
            // ReportUnhandledError will also call this in APPX scenarios,
            // but WinRT can failfast before we get another chance
            // (in APPX scenarios, this one will get overwritten by the one with the CCW pointer)
            GenerateExceptionInformationForDump(exception, IntPtr.Zero);

            // If possible report the exception to GEH, if not fail fast.
            WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;

            if (callbacks == null || !callbacks.ReportUnhandledError(exception))
            {
                FailFast(GetStringForFailFastReason(RhFailFastReason.PN_UnhandledException), exception);
            }
        }
Example #5
0
        /// <summary>
        /// This method checks whether the target to the weakreference is a native COMObject in which case the native object might still be alive although the RuntimeHandle could be null.
        /// Hence we check in the conditionalweaktable maintained by the System.private.Interop.dll that maps weakreferenceInstance->nativeComObject to check whether the native COMObject is alive or not.
        /// and gets\create a new RCW in case it is alive.
        /// </summary>
        /// <returns></returns>
        private Object TryGetComTarget()
        {
#if ENABLE_WINRT
            WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
            if (callbacks != null)
            {
                return(callbacks.GetCOMWeakReferenceTarget(this));
            }
            else
            {
                Debug.Assert(false, "WinRTInteropCallback is null");
            }
#endif // ENABLE_WINRT
            return(null);
        }
Example #6
0
        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);
        }
Example #7
0
        // Only call SetAppXConfiguration from ResourceManager constructors, and nowhere else.
        // Throws MissingManifestResourceException and WinRT HResults
        private void SetAppXConfiguration()
        {
            Debug.Assert(UseUapResourceManagement == false); // Only this function writes to this member
            Debug.Assert(_WinRTResourceManager == null);     // Only this function writes to this member
            Debug.Assert(_PRIonAppXInitialized == false);    // Only this function writes to this member
            Debug.Assert(_PRIExceptionInfo == null);         // Only this function writes to this member

            bool bUsingSatelliteAssembliesUnderAppX = false;

            if (MainAssembly != null)
            {
                if (MainAssembly != typeof(object).Assembly) // We are not loading resources for System.Private.CoreLib
                {
#if ENABLE_WINRT
                    WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
                    if (callbacks != null && callbacks.IsAppxModel())
#else
                    if (ApplicationModel.IsUap)
#endif
                    {
                        // If we have the type information from the ResourceManager(Type) constructor, we use it. Otherwise, we use BaseNameField.
                        string reswFilename = _locationInfo == null ? BaseNameField : _locationInfo.FullName;

                        // The only way this can happen is if a class inherited from ResourceManager and
                        // did not set the BaseNameField before calling the protected ResourceManager() constructor.
                        // For other constructors, we would already have thrown an ArgumentNullException by now.
                        // Throwing an ArgumentNullException now is not the right thing to do because technically
                        // ResourceManager() takes no arguments, and because it is not documented as throwing
                        // any exceptions. Instead, let's go through the rest of the initialization with this set to
                        // an empty string. We may in fact fail earlier for another reason, but otherwise we will
                        // throw a MissingManifestResourceException when GetString is called indicating that a
                        // resW filename called "" could not be found.
                        if (reswFilename == null)
                        {
                            reswFilename = string.Empty;
                        }

                        if (!bUsingSatelliteAssembliesUnderAppX)
                        {
                            UseUapResourceManagement = !ShouldUseSatelliteAssemblyResourceLookupUnderAppX(MainAssembly);

                            if (UseUapResourceManagement)
                            {
                                // Only now are we certain that we need the PRI file.

                                // At this point it is important NOT to set UseUapResourceManagement to false
                                // if the PRI file does not exist because we are now certain we need to load PRI
                                // resources. We want to fail by throwing a MissingManifestResourceException
                                // if WindowsRuntimeResourceManager.Initialize fails to locate the PRI file. We do not
                                // want to fall back to using satellite assemblies anymore. Note that we would not throw
                                // the MissingManifestResourceException from this function, but from GetString. See the
                                // comment below on the reason for this.

                                _WinRTResourceManager = GetWinRTResourceManager();

                                try
                                {
                                    _PRIonAppXInitialized = _WinRTResourceManager.Initialize(MainAssembly.Location, reswFilename, out _PRIExceptionInfo);
                                    // Note that _PRIExceptionInfo might be null - this is OK.
                                    // In that case we will just throw the generic
                                    // MissingManifestResource_NoPRIresources exception.
                                    // See the implementation of GetString for more details.
                                }
                                // We would like to be able to throw a MissingManifestResourceException here if PRI resources
                                // could not be loaded for a recognized reason. However, the ResourceManager constructors
                                // that call SetAppXConfiguration are not documented as throwing MissingManifestResourceException,
                                // and since they are part of the portable profile, we cannot start throwing a new exception type
                                // as that would break existing portable libraries. Hence we must save the exception information
                                // now and throw the exception on the first call to GetString.
                                catch (FileNotFoundException)
                                {
                                    // We will throw MissingManifestResource_NoPRIresources from GetString
                                    // when we see that _PRIonAppXInitialized is false.
                                }
                                catch (Exception e)
                                {
                                    // ERROR_MRM_MAP_NOT_FOUND can be thrown by the call to ResourceManager.get_AllResourceMaps
                                    // in WindowsRuntimeResourceManager.Initialize.
                                    // In this case _PRIExceptionInfo is now null and we will just throw the generic
                                    // MissingManifestResource_NoPRIresources exception.
                                    // See the implementation of GetString for more details.
                                    if (e.HResult != HResults.ERROR_MRM_MAP_NOT_FOUND)
                                    {
                                        throw; // Unexpected exception code. Bubble it up to the caller.
                                    }
                                }

                                if (!_PRIonAppXInitialized)
                                {
                                    UseUapResourceManagement = false;
                                }
                                // Allow all other exception types to bubble up to the caller.

                                // Yes, this causes us to potentially throw exception types that are not documented.

                                // Ultimately the tradeoff is the following:
                                // -We could ignore unknown exceptions or rethrow them as inner exceptions
                                // of exceptions that the ResourceManager class is already documented as throwing.
                                // This would allow existing portable libraries to gracefully recover if they don't care
                                // too much about the ResourceManager object they are using. However it could
                                // mask potentially fatal errors that we are not aware of, such as a disk drive failing.


                                // The alternative, which we chose, is to throw unknown exceptions. This may tear
                                // down the process if the portable library and app don't expect this exception type.
                                // On the other hand, this won't mask potentially fatal errors we don't know about.
                            }
                        }
                    }
                }
            }
            // MainAssembly == null should not happen but it can. See the comment on Assembly.GetCallingAssembly.
            // However for the sake of 100% backwards compatibility on Win7 and below, we must leave
            // _bUsingModernResourceManagement as false.
        }