Exemplo n.º 1
0
        /// <summary>
        /// Try to detect the addin is loaded from system hive key
        /// </summary>
        /// <returns>null if unkown or true/false</returns>
        private bool?IsLoadedFromSystem()
        {
            if (null != _isLoadedFromSystem)
            {
                return(_isLoadedFromSystem);
            }

            ApplicationIdentifiers.ApplicationType applicationType =
                ApplicationIdentifiers.IsApplication(Application.UnderlyingType.GUID);
            if (applicationType == ApplicationIdentifiers.ApplicationType.None)
            {
                return(null);
            }

            OfficeApi.Tools.Contribution.RegistryLocationResult result = OfficeApi.Tools.Contribution.CommonUtils.TryFindAddinLoadLocation(Type,
                                                                                                                                           applicationType);
            switch (result)
            {
            case Office.Tools.Contribution.RegistryLocationResult.User:
                return(false);

            case Office.Tools.Contribution.RegistryLocationResult.System:
                return(true);

            default:
                throw new IndexOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an registry tweak entry in the current addin key
        /// </summary>
        /// <param name="applicationType">target office application</param>
        /// <param name="addinType">addin class type informations</param>
        /// <param name="name">name for the tweak</param>
        /// <param name="value">value for the tweak</param>
        /// <param name="throwException">throw exception on error</param>
        /// <returns>true if value was stored otherwise false</returns>
        public static bool SetTweakPersistenceEntry(ApplicationIdentifiers.ApplicationType applicationType, Type addinType, string name, string value, bool throwException)
        {
            try
            {
                if (null == addinType)
                {
                    return(false);
                }
                RegistryLocationAttribute location = AttributeReflector.GetRegistryLocationAttribute(addinType);
                ProgIdAttribute           progID   = AttributeReflector.GetProgIDAttribute(addinType);

                OfficeApi.Tools.Contribution.RegistryLocationResult addinLocation =
                    Tools.Contribution.CommonUtils.TryFindAddinLoadLocation(addinType, applicationType);
                if (addinLocation == Office.Tools.Contribution.RegistryLocationResult.Unknown)
                {
                    return(false);
                }

                RegistryKey regKey = null;
                switch (addinLocation)
                {
                case Office.Tools.Contribution.RegistryLocationResult.User:
                    regKey = Registry.LocalMachine.OpenSubKey(_addinOfficeRegistryKey + progID.Value, true);
                    break;

                case Office.Tools.Contribution.RegistryLocationResult.System:
                    regKey = Registry.CurrentUser.OpenSubKey(_addinOfficeRegistryKey + progID.Value, true);
                    break;
                }

                if (null == regKey)
                {
                    return(false);
                }

                regKey.SetValue(name, value);
                regKey.Close();

                return(true);
            }
            catch (Exception exception)
            {
                NetOffice.DebugConsole.Default.WriteException(exception);
                if (throwException)
                {
                    throw;
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Try to detect the registry end for the current loaded host application(unkown)
 /// </summary>
 /// <param name="applicationProxy">application proy</param>
 /// <returns>Application name or null if failed</returns>
 private string TryDetectHostRegistryKey(object applicationProxy)
 {
     ApplicationIdentifiers.ApplicationType applicationType =
         ApplicationIdentifiers.IsApplication(applicationProxy.GetType().GUID);
     if (applicationType == ApplicationIdentifiers.ApplicationType.None)
     {
         return(null);
     }
     else
     {
         return(ApplicationIdentifiers.ConvertApplicationType(applicationType));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Try to detect the registry load location from an addin
        /// </summary>
        /// <returns>load location or information it is unable to detect</returns>
        public RegistryLocationResult TryFindAddinLoadLocation()
        {
            try
            {
                Type addinType = null;
                if (null != OwnerType)
                {
                    addinType = OwnerType;
                }
                else if (null != Owner)
                {
                    addinType = Owner.GetType();
                }
                else
                {
                    return(RegistryLocationResult.Unknown);
                }

                System.Runtime.InteropServices.ProgIdAttribute progId =
                    NetOffice.Tools.AttributeReflector.GetProgIDAttribute(addinType, false);
                if (null == progId)
                {
                    return(RegistryLocationResult.Unknown);
                }

                ApplicationIdentifiers.ApplicationType applicationType =
                    ApplicationIdentifiers.IsApplication(OwnerApplication.UnderlyingType.GUID);
                if (ApplicationIdentifiers.ApplicationType.None == applicationType)
                {
                    return(RegistryLocationResult.Unknown);
                }

                return(CommonUtils.TryFindAddinLoadLocation(addinType, applicationType));
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Try to detect the registry load location from an addin
        /// </summary>
        /// <param name="addinType">addin class type informations</param>
        /// <param name="applicationType">which office application</param>
        /// <returns>location or unkown</returns>
        public static RegistryLocationResult TryFindAddinLoadLocation(Type addinType, ApplicationIdentifiers.ApplicationType applicationType)
        {
            try
            {
                System.Runtime.InteropServices.ProgIdAttribute progId =
                    NetOffice.Tools.AttributeReflector.GetProgIDAttribute(addinType, false);
                if (null == progId)
                {
                    return(RegistryLocationResult.Unknown);
                }

                string path = String.Format("Software\\Microsoft\\Office\\{0}\\Addins",
                                            ApplicationIdentifiers.ConvertApplicationType(applicationType));

                Microsoft.Win32.RegistryKey key =
                    Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path, false);
                if (null != key)
                {
                    key.Close();
                    return(RegistryLocationResult.User);
                }

                try
                {
                    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path, false);
                    if (null != key)
                    {
                        key.Close();
                        return(RegistryLocationResult.System);
                    }
                }
                catch (System.Security.SecurityException)
                {
                    return(RegistryLocationResult.Unknown);
                }
                catch
                {
                    throw;
                }

                return(RegistryLocationResult.Unknown);
            }
            catch (Exception)
            {
                return(RegistryLocationResult.Unknown);
            }
        }